List array duplicates with count

后端 未结 7 2002
孤街浪徒
孤街浪徒 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:33

    a little error above, right code is:

    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=" + c;
    
    foreach(string str in results.Distinct())
        Console.WriteLine(str);
    

提交回复
热议问题