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

The Archive Base Latest Questions

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

I wanted to implement an algorithm with Dictionary<Dictionary<char,int>, List<string>> to find the anagram words

  • 0

I wanted to implement an algorithm with Dictionary<Dictionary<char,int>, List<string>> to find the anagram words in a dictionary.

As i need to implement my custom EqualityComparer for this Dictionary, is the access time still O(1) i.e big O (1) ?

Second question, As part of the EqualityComparer I also need to implement the GetHashCode(). What is the efficient way of determining GetHashCode() for Dictionary<Dictionary<char,int>, List<string>>?

i just came up with this method, is there any better alternative?

public int GetHashCode(Dictionary<char, int> obj)
    {
        unchecked
        {
            int hashCode = 17;
            foreach (var item in obj)
            {
                hashCode += 23 * item.Key.GetHashCode();
            }
            return hashCode;
        }
    }

Any word of advice is appreciated. Thanks!

  • 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-28T03:33:58+00:00Added an answer on May 28, 2026 at 3:33 am

    How about converting the word “need” into the string “d1e2n1” instead of using a Dictionary as key? In order to build this string, you could use a binary tree. A char would be used as key and the character count as value. The binary tree is automatically sorted by the key, which is not the case for a dictionary.

    You can calculate a combined hash value from single hash values by combining their binary representation with the XOR operation. With C#, you would do something like this:

    public override int GetHashCode()
    {
        // Combine hashcode of a and b
        return a.GetHashCode() ^ b.GetHashCode();
    }
    

    Finding an entry in an unsorted list is an O(n) operation. Finding an entry in a sorted list is an O(log(n)) operation, if a binary search is used.

    Finding a word in a list in a dictionary is an O(1 + n) operation, which is the same as an O(n) operation, or an O(1 + log(n)) operation, which is the same as an O(log(n)) operation.


    EDIT:

    Here is a possible implementation:

    var anagrams = new Dictionary<string, List<string>>();
    foreach (string word in words) {
        string key = GetFrequency(word);
        List<string> list;
        if (anagrams.TryGetValue(key, out list)) {
            list.Add(word);
        } else {
            list = new List<string> { word };
            anagrams.Add(key, list);
        }
    }
    

    It uses this method to get the key:

    private string GetFrequency(string word)
    {
        var dict = new SortedDictionary<char, int>(); // Implemented as binary tree
        foreach (char c in word.ToLower()) {
            int count;
            if (dict.TryGetValue(c, out count)) {
                dict[c] += 1;
            } else {
                dict[c] = 1;
            }
        }
        return dict.Aggregate(new StringBuilder(), (sb, item) => sb.Append(item.Key).Append(item.Value), sb => sb.ToString());
    }
    

    Using this definition for the words …

    var words = new List<string> { "need", "eden", "team", "meat", "meta", "Nat", "tan" };
    

    This test …

    foreach (var item in anagrams.OrderBy(x => x.Key)) {
        Console.WriteLine();
        Console.WriteLine(item.Key + ":");
        foreach (string word in item.Value.OrderBy(w => w)) {
            Console.WriteLine("    " + word);
        }
    }
    

    … produces this output

    a1e1m1t1:
        meat
        meta
        team
    
    a1n1t1:
        Nat
        tan
    
    d1e2n1:
        eden
        need
    

    EDIT #2:

    Here is the frequency calculation as suggest by Ben Voigt

    private string GetFrequencyByBenVoigt(string word)
    {
        char[] chars = word.ToLower().ToCharArray();
        Array.Sort(chars);
        return new string(chars);
    }
    

    The test result would be

    aemt:
        meat
        meta
        team
    
    ant:
        Nat
        tan
    
    deen:
        eden
        need
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I wanted to implement the usual dynamic programming algorithm for Levensthein distance
this guide is given by one of my lecturer and i wanted to implement
I have been trying to implement an efficient string comparing algorithm that will given
I wanted to implement python com server using win32com extensions. Then consume the server
I wanted to implement the game Pacman. For the AI, I was thinking of
Let's say you wanted to implement a breadth-first search of a binary tree recursively
Since this function is implemented in IE8, I wanted to see exactly what I
Internally speaking, which algorithm(s) does PHP use to implement the various sort functions it
I'm currently looking into quick graph because I need to implement Job Shop Scheduling.
I'm currently implementing a poor-man's version of the RSA Algorithm and I wanted the

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.