C#: How to find the frequency of the content within a LinkedList using a Hashtable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ArrayListFreq
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“indeed”);

LinkedList elementList = new LinkedList();

elementList.AddLast(“qwerty”);
elementList.AddLast(“qwerty”);
elementList.AddLast(“fox”);
elementList.AddLast(“one”);
elementList.AddLast(“7”);
elementList.AddLast(“qwerty”);
elementList.AddLast(“fox”);
elementList.AddLast(“!”);

Hashtable frequencyHash = new Hashtable();

LinkedList uniqueList = new LinkedList();

foreach (var element in elementList)
{

if (uniqueList.Contains(element))
{
int elementCount = int.Parse(frequencyHash[element].ToString());
elementCount++;

frequencyHash[element] = elementCount;
}
else
{
uniqueList.AddLast(element);
frequencyHash.Add(element, 1);
}
}
foreach (string element in frequencyHash.Keys)
{
Console.WriteLine(String.Format(“{0}: {1}”, element, frequencyHash[element]));
}
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *