1

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?

Sandar Min Aye
  • 499
  • 6
  • 15
  • 28
  • 1
    How do you define no highest value - is it when all values are same or something else? – shree.pat18 May 20 '16 at 07:07
  • 1
    How do you define `no highest value`? If all are the same / all are `0` or if more than x of them have the same highest value? If more than one has `the highest value`, which one wins? The last, the one with the `highest` Key/ etc ? – Manfred Radlwimmer May 20 '16 at 07:08
  • probably the highest value must be unique – mihail May 20 '16 at 07:09
  • 1
    @mihail case 2 has 2 values with value `1` and `BBB` is returned. – Dovydas Šopa May 20 '16 at 07:10
  • @mihail Not according to the examples. They don't cover everything though so a `definition` from OP would be nice – Manfred Radlwimmer May 20 '16 at 07:10
  • You probably could group the keys after ordering and check if the first key has member > 1 – Ian May 20 '16 at 07:10
  • @Ian Although that's a possibility, I think it's best not to speculate and wait for an answer from OP. There are way to many ways to interpret this question. – Manfred Radlwimmer May 20 '16 at 07:11
  • 1
    `If there is no highest value in a dictionary, I want to return null or "".` - I understand it as if the max value found occurs more than once it's not considered as "max value" – mihail May 20 '16 at 07:11
  • Sorry I made confuse my question. As @mihail said, I want to return null or something if the max value found more than one time. – Sandar Min Aye May 20 '16 at 07:20
  • @Sandar Min Aye So your case 2 example is invalid? – Dovydas Šopa May 20 '16 at 07:21
  • in this case you probably want to remove all duplicates from your dictionary - refer to this question http://stackoverflow.com/questions/1606679/remove-duplicates-in-the-list-using-linq – mihail May 20 '16 at 07:21
  • or to be more accurate, find some max key, remove the duplicates and check if the max key you have is still presented when you have removed the duplicates – mihail May 20 '16 at 07:24

4 Answers4

1

You could do something like this.

var maxKey = dictionary.Aggregate((l, r) => l.Value > r.Value ? l : r);

if(dictionary.Values.Where(x=>x == maxKey.Value).Count() >1)
{
    ...
    return null;
} 

return maxKey.Key; 
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
0

Write another line.

var maxkv = dictionary.Aggregate((l, r) => l.Value > r.Value ? l : r);
var maxKey = dictionary.Where(k => k.Value == maxkv.Value).Take(2).Count() > 1 ? null : maxkv.Key;

This will throw exception if your dictionary is empty so do check for length before this if necessary.

if(dictionary.Count == 0) return; // or some guard like this.

If your dictionary is not empty then you will always have at least one item to take. try to take another item (Take(2)) so if count becomes 2 means there are two or more equal keys with max value.

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
0

You can use GroupBy to group the items based on their Value, then get the Group with highest value and check for Items Count

var Dictionary = new Dictionary<string, int>();
int? maxValue = null;
var MaxGroup = Dictionary.GroupBy(a => a.Value).OrderBy(a=>a.Key).First();
if (MaxGroup.Count() == 1)
{
    maxValue = MaxGroup.Key;
}
Kahbazi
  • 14,331
  • 3
  • 45
  • 76
-1

You can use this code:

var dictionary = new Dictionary<string, int> {{"AAA", 1},{"BBB", 0}, {"CCC", 1}};
int maxValue = dictionary.Values.Max();
var maxKVs = dictionary.Where(kv => kv.Value == maxValue);
string keyMax = null;
if (maxKVs.Count() == 1)
{
    keyMax = maxKVs.First().Key;
}
else
{
    // Multiple items with value equals to max
}

It will store the max value of the dictionary then count how many items have the same value and assign keyMax if only one item have this value.


If you want to browse the dictionary only once, use this foreach:

var dictionary = new Dictionary<string, int> {{"AAA", 1},{"BBB", 0}, {"CCC", 1}};
int maxValue = Int32.MinValue;
string maxKey = null;
foreach (KeyValuePair<string, int> keyValuePair in dictionary)
{
    if (keyValuePair.Value > maxValue)
    {
        maxValue = keyValuePair.Value;
        maxKey = keyValuePair.Key;
    }
    else if (keyValuePair.Value == maxValue)
    {
        maxKey = null; // not max, reset key
    }
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142