List array duplicates with count

后端 未结 7 1970
孤街浪徒
孤街浪徒 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条回答
  • 2020-11-30 09:55

    I think this should do the trick

        string[] arr = { "red", "red", "blue", "green", "Black", "blue", "red" };
    
        var results = from str in arr
                      let c = arr.Count( m => str.Contains(m.Trim()))
                      select str + " count=" + str;
    
        foreach(string str in results.Distinct())
            Console.WriteLine(str);
    

    Output:

    red count=3
    blue count=2
    green count=1
    Black count=1
    
    0 讨论(0)
提交回复
热议问题