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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:49:44+00:00 2026-06-17T09:49:44+00:00

Possible Duplicate: Dictionary returning a default value if the key does not exist I

  • 0

Possible Duplicate:
Dictionary returning a default value if the key does not exist

I have a string that contains only digits. I’m interested in generating a frequency table of the digits. Here’s an example string:

var candidate = "424256";

This code works, but it throws a KeyNotFound exception if I look up a digit that’s not in the string:

var frequencyTable = candidate
    .GroupBy(x => x)
    .ToDictionary(g => g.Key, g => g.Count());

Which yields:

Key Count
4   2 
2   2 
5   1 
6   1 

So, I used this code, which works:

var frequencyTable = (candidate + "1234567890")
    .GroupBy(x => x)
    .ToDictionary(g => g.Key, g => g.Count() - 1);

However, in other use cases, I don’t want to have to specify all the possible key values.

Is there an elegant way of inserting 0-count records into the frequencyTable dictionary without resorting to creating a custom collection with this behavior, such as this?

public class FrequencyTable<K> : Dictionary<K, int>
{
  public FrequencyTable(IDictionary<K, int> dictionary) 
    : base(dictionary)
  { }

  public new int this[K index]
  {
    get
    {
        if (ContainsKey(index))
            return base[index];
        return 0;
    }
  }
}
  • 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-06-17T09:49:45+00:00Added an answer on June 17, 2026 at 9:49 am

    If you do not somehow specify all possible key values, your dictionary will not contain an entry for such keys.

    Rather than storing zero counts, you may wish to use

    Dictionary.TryGetValue(...)
    

    to test the existence of the key before trying to access it. If TryGetValue returns false, simply return 0.

    You could easily wrap that in an extension method (rather than creating a custom collection).

    static public class Extensions
    {
        static public int GetFrequencyCount<K>(this Dictionary<K, int> counts, K value)
        {
            int result;
            if (counts.TryGetValue(value, out result))
            {
                return result;
            }
            else return 0;
        }
    }
    

    Usage:

    Dictionary<char, int> counts = new Dictionary<char, int>();
    counts.Add('1', 42);
    int count = counts.GetFrequencyCount<char>('1');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Getting key with maximum value in dictionary? Let's say I have a
Possible Duplicate: Dictionary With Value “Variable” I have some highly coupled code that I
Possible Duplicate: Why is my object not key value coding-compliant? I'm having a dictionary
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm finding that dictionary
Possible Duplicate: Sort by key of dictionary inside a dictionary in Python I have
Possible Duplicate: Why does this Random Number Generator not random? I have this test
Possible Duplicate: Find longest (string) key in dictionary Without folding. Example: from functools import
Possible Duplicate: How to make a python dictionary that returns key for keys missing
Possible Duplicate: Modifying .NET Dictionary while Enumerating through it I have a dictionary object
Possible Duplicate: Which collection for storing unique strings? I am currently using a Dictionary<string,

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.