如何从数组中获取多个最常见的值并将它们添加到另一个?

时间:2021-07-26 12:21:30

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)

..获得第一个最常见的值(在这种情况下:2)

What if I want to get multiple values (in this case: 2,3,1) ?

如果我想获得多个值(在这种情况下:2,3,1)怎么办?

I need to add this values to another temporary array to check if this tempArray.Count > 1.

我需要将此值添加到另一个临时数组以检查此tempArray.Count是否> 1。

3 个解决方案

#1


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.

请注意,检查tmp列表计数是否为非零是不必要的,因为执行TakeWhile条件的唯一方法是临时列表中至少有一个项目。换句话说,当tmp为空时,lambda条件p => p.Count == tmp [0]。永远不会达到.Count,并且永远不会抛出超出范围的异常。

#2


1  

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);

#3


0  

If you are saying you want the top 3 results:

如果你说你想要前3个结果:

var query = array.GroupBy(item => item)
  .OrderByDescending(g => g.Count())
  .Select(g => g.Key).Take(3);

#1


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.

请注意,检查tmp列表计数是否为非零是不必要的,因为执行TakeWhile条件的唯一方法是临时列表中至少有一个项目。换句话说,当tmp为空时,lambda条件p => p.Count == tmp [0]。永远不会达到.Count,并且永远不会抛出超出范围的异常。

#2


1  

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);

#3


0  

If you are saying you want the top 3 results:

如果你说你想要前3个结果:

var query = array.GroupBy(item => item)
  .OrderByDescending(g => g.Count())
  .Select(g => g.Key).Take(3);