Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return number of matches from c# dictionary

Tags:

c#

collections

I have a dictionary with non unique values and I want to count the matches of a string versus the values.

Basically I now do dict.ContainsValue(a) to get a bool telling me if the string a exists in dict, but I want to know not only if it exists but how many times it exists (and maybee even get a list of the keys it exists bound to)

Is there a way to do this using dictionary, or should I look for a different collection?

/Rickard Haake

like image 855
Rickard Haake Avatar asked May 26 '10 11:05

Rickard Haake


1 Answers

To get the number of instances of the value you could do something like this:

dict.Values.Count(v => v == a);

To find the keys that have this value you could do this:

dict.Where(kv => kv.Value == a).Select(kv => kv.Key);
like image 185
Simon Steele Avatar answered Sep 20 '22 17:09

Simon Steele