How get key of highest value of a dictionary in C#

5 Answers

0 votes
using System;
using System.Linq;
using System.Collections.Generic;
    
class Program
{
    static void Main() {
         var dict = new Dictionary<string, int> {["c#"] = 6, 
                                                 ["c++"] = 8,
                                                 ["rust"] = 3,
                                                 ["c"] = 9,
                                                 ["php"] = 5,
                                                 ["java"] = 4 };

        Console.WriteLine(dict.Aggregate((x, y) => x.Value > y.Value ? x : y).Key);
        Console.WriteLine(dict.Aggregate((x, y) => x.Value > y.Value ? x : y).Value);
    }
}
    
    
    
    
    
/*
run:
       
c
9
     
*/

 



answered Aug 11, 2023 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;
    
class Program
{
    static void Main() {
         var dict = new Dictionary<string, int> {["c#"] = 6, 
                                                 ["c++"] = 8,
                                                 ["rust"] = 3,
                                                 ["c"] = 9,
                                                 ["php"] = 5,
                                                 ["java"] = 4 };

        Console.WriteLine(dict.OrderByDescending(x => x.Value).First().Key);
        Console.WriteLine(dict.OrderByDescending(x => x.Value).First().Value);
    }
}
    
    
    
    
    
/*
run:
       
c
9
     
*/

 



answered Aug 11, 2023 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;
    
class Program
{
    static void Main() {
         var dict = new Dictionary<int, string> {[6] = "c#", 
                                                 [8] = "c++",
                                                 [3] = "rust",
                                                 [9] = "c" ,
                                                 [5] = "php",
                                                 [4] = "java"};

        Console.WriteLine(dict.OrderByDescending(x => x.Value).First().Key);
        Console.WriteLine(dict.OrderByDescending(x => x.Value).First().Value);
    }
}
    
    
    
    
    
/*
run:
       
3
rust
     
*/

 



answered Aug 11, 2023 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;
    
class Program
{
    static void Main() {
         var dict = new Dictionary<int, string> {[6] = "c#", 
                                                 [8] = "c++",
                                                 [3] = "rust",
                                                 [9] = "c" ,
                                                 [5] = "php",
                                                 [4] = "java"};

        Console.WriteLine(dict.Values.Max());
    }
}
    
    
    
    
    
/*
run:
       
rust
     
*/

 



answered Aug 11, 2023 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;
    
class Program
{
    static void Main() {
        var dict = new Dictionary<string, int> { ["c#"] = 6, 
                                                 ["c++"] = 8,
                                                 ["rust"] = 3,
                                                 ["c"] = 9,
                                                 ["php"] = 5,
                                                 ["java"] = 4 };

        Console.WriteLine(dict.Values.Max());
    }
}
    
    
    
    
    
/*
run:
       
9
     
*/

 



answered Aug 11, 2023 by avibootz

Related questions

2 answers 87 views
2 answers 140 views
2 answers 110 views
1 answer 141 views
141 views asked Oct 17, 2018 by avibootz
...