Alex Rivera | Logout

List array duplicates with count

Asked 2011-10-20T07:41:02.097
13

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 value of array, for example:

red    Count=3
blue   Count=2
Green  Count=1
White  Count=1
Grey   Count=1
Edit
Report

1 Answer

-1

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
answered 2011-10-20T07:55:37.330

Your Answer