I have a dictionary like this.
var dictionary = new Dictionary<string, int>();
dictionary.Add(string, int);
....
....
To get the key of the highest value of a dictionary in c#, I wrote as below.
String maxKey = dictionary.Aggregate((l, r) => l.Value > r.Value ? l : r).Key;
Currently I get below result using above code.
In Case 1, maxKey is "BBB"
dictionary => [0] {["AAA",0]]
[1] {["BBB",1]]
[2] {["CCC",0]]
In Case 2, maxKey is "BBB"
dictionary => [0] {["AAA",1]]
[1] {["BBB",1]]
[2] {["CCC",0]]
In Case 3, maxKey is "CCC"
dictionary => [0] {["AAA",0]]
[1] {["BBB",0]]
[2] {["CCC",0]]
If there is no highest value in a dictionary (if the highest value found more than one time, it's not mean as "max value") , I want to return null or "".
If there is highest value in a dictionary, I want to return key of the highest value.
Can anybody tell me a better way to do this?