如何在给定的十进制数组中获得高次数的重复元素... [英] How to get highet number of time repeated element in an given decimal array...
本文介绍了如何在给定的十进制数组中获得高次数的重复元素...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我已经给出了十进制数组的集合
decimal [] data = new decimal [] {1,5,4,8,11,1,2,2,3,3,3我想在上面给出的数组中获得最大数量的重复值
例如在上面的数组元素3中重复了3次,所以输出将是3.我希望通过Linq或Lamda表达来达到这个目的。
类似
Hi I have given set of decimal array
decimal[] data = new decimal[]{1, 5, 4, 8, 11, 1, 2, 2, 3, 3, 3};
I want to get the highest number of repeated value in the above given array
for e.g in above array element 3 has been repeated 3 time so the output would be 3. I want to achieve this by Linq or Lamda expression.
something like
IEnumerable<string> query = data.OrderByDescending(n => n.Count()).Select(n => n).first()
;
提前致谢
;
Thanks in advance
推荐答案
这将为您提供每个值的计数:
This will give you each value with it's count:
var counts = data.GroupBy(x => x).Select(g => new { count = g.Count(), value = g.Key });
所以你可以使用Max方法:
So you can just use the Max method:
int highestCount = data.GroupBy(x => x).Select(g => new { count = g.Count(), value = g.Key }).Max(g => g.count);
甚至:
Or even:
int highestCount = data.GroupBy(x => x).Select(g => g.Count()).Max();
这篇关于如何在给定的十进制数组中获得高次数的重复元素...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文