My Technical Notes

Wednesday, 4 March 2015

C#: Getting most common integer in a list of integers


var integers = new[] 
{
    10, 10, 10, 10, 11, 11, 11, 11, 11
};

var mostCommonInteger = 
    integers
    .GroupBy(x => x)
    .ToDictionary(x => x.Key, x => x.Count())
    .OrderByDescending(x => x.Value)
    .First().Key;

No comments: