I have an array like this:
[2 1 2 4 3 3 1]
I'm using this...
var query = array.GroupBy(item => item)
    .OrderByDescending(g => g.Count())
    .Select(g => g.Key)
    .First();
.. to get first most common value (in this case : 2)
What if I want to get multiple values (in this case: 2,3,1) ?
I need to add this values to another temporary array to check if this tempArray.Count > 1.
If you would like to get all groups tied for the top count, you could do it like this:
var tmp = array
   .GroupBy(item => item)
   .OrderByDescending(g => g.Count())
   .Select(g => new {
       Item = g.Key
   ,   Count = g.Count()
   }).ToList();
var res = tmp
    .TakeWhile(p => p.Count == tmp[0].Count)
    .Select(p => p.Item)
    .ToList();
Note that the check for tmp list count to be non-zero is unnecessary, because the only way the TakeWhile condition is executed is when there is at least a single item in the temporary list. In other words, when tmp is empty, the lambda condition p => p.Count == tmp[0].Count is never reached, and the out-of-range exception is never thrown.
This is how I would approach it (splitting out the code for clarity):
//Basic query
var query = array
    .GroupBy(i => i);
//Get the maximum count number
var maxCount = query
    .Max(i => i.Count());
//Get all the values that have a count of maxCount
var result = query
    .Where(i => i.Count() == maxCount)
    .Select(i => i.Key);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With