Find key with max value from SortedDictionary?

c#, max, sorteddictionary

Solution

If `dict` is your `SortedDictionary<,>` (or any `IDictionary<,>`) and you want all the keys that correspond to the max value, first check that `dict` is not null or empty (you need at least one element). Then maybe this works:

var max = dict.Values.Max();
var relevantKeys = dict.Where(pair => max.Equals(pair.Value))
    .Select(pair => pair.Key);

Maybe it can be done more efficiently?

Problem

I have a `SortedDictionary` how do I find the key associated with the max value? Do I have to loop through every KeyValuePair?

Original source

Related problems