Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get multiple most commons values from array and add them to another?

Tags:

arrays

c#

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.

like image 227
abecadlo37 Avatar asked Jul 22 '15 15:07

abecadlo37


2 Answers

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.

like image 78
Sergey Kalinichenko Avatar answered Nov 10 '22 13:11

Sergey Kalinichenko


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);
like image 42
DavidG Avatar answered Nov 10 '22 14:11

DavidG