I have this one problem. There is a string
string [5] names = { "John", "Sam", "Harry", "Sam", "John" }
I need to find the most common elements in the array. I tried using :
string MostCommon = names.GroupBy(v => v)
.OrderByDescending(g => g.Count())
.First()
.Key;
Unfortunately it only finds one element, f.e., MostCommon = John, and in this case I need not only John, but Sam too. How could I do that? Maybe LINQ is not necessary in this case?
.First()is your problem. Take off the.First(), and you'll have more than one result, but you won't know what the specific counts are with that single LINQ statement.