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

  • SEARCH
  • Home
  • 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 551169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:21:54+00:00 2026-05-13T11:21:54+00:00

edit: Thanks Jason, the fact that it was a dictionary isn’t that important. I

  • 0

edit: Thanks Jason, the fact that it was a dictionary isn’t that important. I just wanted the runtime to have a low runtime. Is that LINQ method fast? Also, I know this is off topic but what does the n => n mean?

I have a list of numbers and I want to make another list with the numbers that appear most at the beginning and the least at the end.

So what I did was when through the list and checked if the number x was in the dictionary. If it wasn’t then I made the key x and the value one. If it was then I changed the value to be the value plus one.

Now I want to order the dictionary so that I can make a list with the ones that appear the most at the beginning and the least at the end.

How can I do that in C#?
ps. runtime is very important.

  • 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-13T11:21:55+00:00Added an answer on May 13, 2026 at 11:21 am

    So it sounds like you have a Dictionary<int, int> where the key represents some integer that you have in a list and corresponding value represents the count of the number of times that integer appeared. You are saying that you want to order the keys by counts sorted in descending order by frequency. Then you can say

    // dict is Dictionary<int, int>
    var ordered = dict.Keys.OrderByDescending(k => dict[k]).ToList();
    

    Now, it sounds like you started with a List<int> which are the values that you want to count and order by count. You can do this very quickly in LINQ like so:

    // list is IEnumerable<int> (e.g., List<int>)
    var ordered = list.GroupBy(n => n)
                      .OrderByDescending(g => g.Count())
                      .Select(g => g.Key)
                      .ToList();
    

    Or in query syntax

    var ordered = (from n in list
                   group n by n into g
                   orderby g.Count() descending
                   select g.Key).ToList();
    

    Now, if you need to have the intermediate dictionary you can say

    var dict = list.GroupBy(n => n)
                   .ToDictionary(g => g.Key, g => g.Count());
    var ordered = dict.Keys.OrderByDescending(k => dict[k]).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please let me know if you have any idea about it. Thanks EDIT What
Why new()/delete() is slower than malloc()/free()? EDIT: Thanks for the answers so far. Please
EDIT: SOLVED Thanks Brooks. Your question led me to keep digging into if the
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
EDIT: I have fixed all but two warnings now, so thank you all for
EDIT: Thanks Kenny and everyone else who answered similarly. It really was a DOH
I have a multidimensional array that looks something like this: ourThing = array( 'id'
I have an app that works fine. I'm trying to make one of the
I have set caching on a query that is getting run a lot of
I have a Python set that contains objects with __hash__ and __eq__ methods in

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.