List array duplicates with count

后端 未结 7 2010
孤街浪徒
孤街浪徒 2020-11-30 09:06

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 val

7条回答
  •  Happy的楠姐
    2020-11-30 09:53

    LINQ makes this easy:

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

提交回复
热议问题