List array duplicates with count

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

    Hmm That is a very hard task, but Captain Algorithm will help you! He is telling us that there are many ways to do this. One of them he give me and I give it to you:

    Dictionary  tmp = new Dictionary  ();
    
    foreach (Object obj in YourArray)
      if (!tmp.ContainsKey(obj))
        tmp.Add (obj, 1);
     else tmp[obj] ++;
    
    tmp.Values;//Contains counts of elements
    

提交回复
热议问题