Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7065567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:58:19+00:00 2026-05-28T04:58:19+00:00

First, have a look at this code: Dictionary<int,int> dict = Dictionary<int,int>(); dict[3] = 1;

  • 0

First, have a look at this code:

Dictionary<int,int> dict = Dictionary<int,int>();
dict[3] = 1;
dict[2] = 2;
dict[1] = 3;

foreach(KeyValuePair<int,int> item in dict.OrderByDescending(p => p.Value))
{
    print(item.Value);
    break;
}

This code, basically prints the value of the entry in the dictionary with the highest value. I’d like to accomplish this without using a “broken” foreach loop. How might I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:58:20+00:00Added an answer on May 28, 2026 at 4:58 am

    Well, you could do:

    if(dict.Any())
       print(dict.Values.Max());
    

    This is not only more concise, but also doesn’t require sorting the dictionary out-of-place first (which is what starting an enumeration onOrderByDescending does), so is more efficient in both time and space.

    If you need the key as well, you can use a MaxBy operator (such as from moreLinq) as follows:

    if(dict.Any())
    {
        var bestKvp = dict.MaxBy(kvp => kvp.Value);
        Console.WriteLine("Key = {0}, Value = {1}", bestKvp.Key, bestKvp.Value);
    }
    

    It is possible to accomplish this with standard LINQ to Objects in O(n) time and O(1) space with the Aggregate operator, but it’s quite ugly:

    if(dict.Any())
    {
        var bestKvp = dict.Aggregate((bestSoFar, next) => bestSoFar.Value > next.Value ? bestSoFar : next );
        Console.WriteLine("Key = {0}, Value = {1}", bestKvp.Key, bestKvp.Value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a look at this F#/OCaml code: type AllPossible = | A of int
Please have a look at this code: Card *first = [[Card alloc] initWithFace:@Ace andSuit:@Hearts];
First of all have a look at the following code (in this code shape
first, have to look at this dragndrop ( http://code.google.com/p/jquery-drag-and-drop/ ) what i want to
Have a look at this code snippet: int i = 10; int *pi =
I have tried this example http://www.bennadel.com/blog/1056-ColdFusion-CFPOP-My-First-Look.htm , but it retrieve emails from POP server.
i have a table (tbl_world) which look like this id | first_name | last_name
I have first: select url from urls where site = '$value' order by url
first I want to say that I hope this doesn't look like I am
I have strings that look like this {/CSDC} CHOC SHELL DIP COLOR {17} I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.