使用计数列出数组重复项 [英] List array duplicates with count

查看:22
本文介绍了使用计数列出数组重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下结果的数组

I have an array which contains the following results

red 
red
red
blue
blue
Green
White
Grey

我想获得数组的每个值的重复计数,例如:

and I want to get duplicate count of every value of array, for example:

red    Count=3
blue   Count=2
Green  Count=1
White  Count=1
Grey   Count=1

推荐答案

LINQ 让这一切变得简单:

LINQ makes this easy:

Dictionary<string, int> counts = array.GroupBy(x => x)
                                      .ToDictionary(g => g.Key,
                                                    g => g.Count());

这篇关于使用计数列出数组重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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