IEnumerable<string> inputList; // input words.
var mostFrequentlyUsed = inputList.GroupBy(word => word)
.Select(wordGroup => new { Word = wordGroup.Key, Frequency = wordGroup.Count() })
.OrderByDescending(word => word.Frequency);
Explanation: I don't really know if it requires further explanation but I'll try. inputList
is an array or any other collection providing source words. GroupBy
function will group the input collection by some similar property (which is, in my code the object itself, as noted by the lambda word => word
). The output (which is a set of groups by a specified key, the word) will be transformed to an object with Word
and Frequency
properties and sorted by Frequency
property in descending order. You could use .Take(10000)
to take the first 10000. The whole thing can be easily parallelized by .AsParallel()
provided by PLINQ. The query operator syntax might look clearer:
var mostFrequentlyUsed =
(from word in inputList
group word by word into wordGroup
select new { Word = wordGroup.Key, Frequency = wordGroup.Count() })
.OrderByDescending(word => word.Frequency).Take(10000);