C#如何用统计数组中相同元素个数

问题描述:

C#如何用统计数组中相同元素个数
如数组string[] value = {“DFF11”,“DFF11”,“RFF11”,“RFF11”,“RFF11”,“CFF11”}如何统计相同元素的个数.

string[] value = new string[] { "DFF11","DFF11","RFF11","RFF11","RFF11","CFF11" };
var source = value.GroupBy(t => t.Trim()).Select(t => new { count = t.Count(),key = t.Key }).ToArray();
foreach (var s in source)
Console.WriteLine(s.key + ":" + s.count);