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 7167775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:38:43+00:00 2026-05-28T14:38:43+00:00

In C# i have a List which contains numbers in string format. Which is

  • 0

In C# i have a List which contains numbers in string format. Which is the best way to count all this numbers? For example to say i have three time the number ten..

I mean in unix awk you can say something like

tempArray["5"] +=1

it is similar to a KeyValuePair but it is readonly.

Any fast and smart way?

  • 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-28T14:38:44+00:00Added an answer on May 28, 2026 at 2:38 pm

    (As noted in digEmAll’s answer, I’m assuming you don’t really care that they’re numbers – everything here assumes that you wanted to treat them as strings.)

    The simplest way to do this is to use LINQ:

    var dictionary = values.GroupBy(x => x)
                           .ToDictionary(group => group.Key, group => group.Count());
    

    You could build the dictionary yourself, like this:

    var map = new Dictionary<string, int>();
    foreach (string number in list)
    {
        int count;
        // You'd normally want to check the return value, but in this case you
        // don't care.
        map.TryGetValue(number, out count);
        map[number] = count + 1;
    }
    

    … but I prefer the conciseness of the LINQ approach 🙂 It will be a bit less efficient, mind you – if that’s a problem, I’d personally probably create a generic “counting” extension method:

    public static Dictionary<T, int> GroupCount<T>(this IEnumerable<T> source)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source");
        }
        var map = new Dictionary<T, int>();
        foreach (T value in source)
        {
            int count;
            map.TryGetValue(number, out count);
            map[number] = count + 1;
        }
        return map;
    }
    

    (You might want another overload accepting an IEqualityComparer<T>.) Having written this once, you can reuse it any time you need to get the counts for items:

    var counts = list.GroupCount();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an List<int> which contains 1,2,4,7,9 for example. I have a range from
I have a List which contains a list of objects and I want to
I have a Request object which contains a list of Approvers. An approver has
I have a base class Node which contains a list of child nodes. Node
I have read the page in Emacs wiki which contains a list of session
I have an array that contains a list of vertices which I copy to
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have a table tblInvestigators which contains a lookup field to display a list
I have a string which contains coordinates of chess pieces: pieces = [Ka4Qb3Td7b4c4] How
I have list in SML, which contains members from datatype expr (list b). 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.