Originally posted by ASB
Just asking opinions on the most efficient and elegant way to achieve the following.
HTH
Hashtable wordMap = new Hashtable();
string[] words= <your array of words here>;
foreach (string currentWord in words)
{
if (currentWord.Length > 0)
{
if (wordMap.ContainsKey(currentWord))
{
wordMap[currentWord] = (int)wordMap[currentWord]+ 1;
}
else
{
wordMap.Add(currentWord, 1);
}
}
}
string[] wordNames = (string[])new ArrayList(wordMap.Keys).ToArray(typeof(string));
int[] wordFrequencies = (int[])new
ArrayList(wordMap.Values).ToArray(typeof(int));
Array.Sort(wordFrequencies, wordNames);
for (int currentWord = 0; currentWord < wordNames.Length; currentWord++)
{
Console.WriteLine((wordNames[currentWord])+("\t")+(wordFrequencies[currentWord].ToString()));
}

Comment