3
Reply

SortedList for Letter Frequency?

Tj

Tj

Apr 8 2010 5:50 PM
3.6k
Just wondering what data type you guys would recommend for counting up the occurrences of letters A->Z in words, and THEN sorting from Most Frequent letter to Least Frequent?

SortedList<string, int> sl = new SortedList<string, int>()
for (char i = 'A'; i <= 'Z'; i++)
{
    sl.Add(i.ToString(), 0);
}


lends itself pretty well to getting a distribution (so that the integer is incremented with each occurrence of 'A', 'B', 'C' etc.)

In the 'olden days' (using C++) I'd probably have sorted a fixed array (26 of a char/int struct) - can you suggest a better way in C#?

Would received wisdom dictate using ICompareTo(), Sort or something?

Answers (3)