返回列表中的最大重复项 [英] Return max repeated item in list

查看:15
本文介绍了返回列表中的最大重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        List<string> prod = new List<string>();
        prod.Add("dfg");
        prod.Add("dfg");
        prod.Add("ojj");
        prod.Add("dfg");
        prod.Add("e");

在上面的代码 prod 列表中有项目dfg"重复三次(最大计数)...我想要dfg"作为输出,因为该项目重复最多次数.任何人都可以帮忙

In the above code prod List has item "dfg" repeated thrice(max count)... I want "dfg" as the output because this item is repeated maximum times. Can anyone help in this

推荐答案

你可以使用 LINQ:

You can use LINQ:

string maxRepeated = prod.GroupBy(s => s)
                         .OrderByDescending(s => s.Count())
                         .First().Key;

这篇关于返回列表中的最大重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆