0

I have a dictionary like so -

public static Dictionary<int, List<int>> pegMap = new Dictionary<int, List<int>>();

Now I have populated the dictionary like say -

Key: 1 => Value: [3,2]
Key: 2 => Value: []
Key: 3 => Value: [6,7]

Now I want to find the key which has the highest value in the list.

Like in this case the the lambda should return 3 which denotes the key-value pair where key is 3 because the number 7 is present in the list in the dictionary where key happens to be 3.

Alex
  • 7,901
  • 1
  • 41
  • 56
Soham Dasgupta
  • 5,061
  • 24
  • 79
  • 125

3 Answers3

3

Its a bit hacky but should work.

var dict = new Dictionary<int, List<int>>();

    dict.Add(1, new List<int>() { 1, 2 });
    dict.Add(2, new List<int>() { 4, 5 });
    dict.Add(3, new List<int>() { 1, 7 });

    var max = dict.Select(x => new { Key = x.Key, Value = x.Value.Max() }).OrderByDescending(x => x.Value).First().Key;  
// returns 3
        // Other sample input 
        dict.Add(1, new List<int>() { 1, 2 });
        dict.Add(2, new List<int>() { 4, 7 });
        dict.Add(3, new List<int>() { 1, 2 });
        // returns 2
        dict.Add(1, new List<int>() { 1, 2 });
        dict.Add(2, new List<int>() { 4, 7 });
        dict.Add(3, new List<int>() { 1, 7 });
        // returns 2
        dict.Add(1, new List<int>() { 1,10 });
        dict.Add(2, new List<int>() { 4, 7 });
        dict.Add(3, new List<int>() { 1, 7 });
        // returns 1

Edit: to the smallest value in the list with the largest value:

 var min_value_in_maxList = dict.Select(x => new { Key = x.Key, ValueMax = x.Value.Max(), ValueMin = x.Value.Min() }).OrderByDescending(x => x.ValueMax).First().ValueMin;
Alex
  • 7,901
  • 1
  • 41
  • 56
  • Can you explain the new keyword in the lambda. I'm not getting that part. – Soham Dasgupta Mar 08 '12 at 12:49
  • In this case, new generates an anonymous type with two properties, Key and Value. See this question for a detailed answer. http://stackoverflow.com/questions/48668/how-should-anonymous-types-be-used-in-c – Alex Mar 08 '12 at 12:52
  • Just one more question what if I want to find the least number in the `List` where max is present. – Soham Dasgupta Mar 08 '12 at 12:55
  • See my edit. Just add another anyonymous variable and query that one in the end. – Alex Mar 08 '12 at 13:08
1

Unfortunately there's nothing built into LINQ to Objects which makes this particularly pleasant. You can use MaxBy from my MoreLINQ project though, with the slight trick of needing to use Max on each list as well:

var maxKey = pegMap.MaxBy(x => x.Value.Max())
                   .Key;

Note that if there are multiple keys which have the same top element in the list, it will return the first one.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

this should work,

pegMap.SelectMany(a => a.Value, (a, b) => new {holdKey = a.Key,listValue= b}).OrderByDescending(a=>a.listValue).First().holdKey;
labroo
  • 2,901
  • 3
  • 27
  • 36
  • :( , sorry almost same as @Alex answer. Didnt refresh before answering – labroo Mar 07 '12 at 07:11
  • Not a problem. I think this is something that [so] should think about. A lot of people, including me, don't refresh before answering or commenting. A real-time problem. – Soham Dasgupta Mar 08 '12 at 12:51