Good way to get the key of the highest value of a Dictionary in C#

前端 未结 10 962
花落未央
花落未央 2020-12-04 17:33

I\'m trying to get the key of the maximum value in the Dictionary results.

This is what I have so far:

double max          


        
10条回答
  •  清歌不尽
    2020-12-04 17:52

    This is a fast method. It is O(n), which is optimal. The only problem I see is that it iterates over the dictionary twice instead of just once.

    You can do it iterating over the dictionary once by using MaxBy from morelinq.

    results.MaxBy(kvp => kvp.Value).Key;
    

提交回复
热议问题