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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:21:52+00:00 2026-05-16T06:21:52+00:00

just wondering:if I have two SortedDictionary objects, what is the fastest way to find

  • 0

just wondering:if I have two SortedDictionary objects, what is the fastest way to find out if their contents are the same?
Looping all keys and checking the values does not sound like the best solution.
Will it be enough to just check GetHashCode()?

Edit: I have tried around a bit. See this code:

SortedDictionary<string, string> o1 = new SortedDictionary<string, string>( );
SortedDictionary<string, string> o2 = new SortedDictionary<string, string>( );
o1["k1"] = "v1";
o1["k2"] = "v2";
o1["k3"] = "v3";

o2["k2"] = "v2";
o2["k1"] = "v1";
o2["k3"] = "v3";

Console.WriteLine( "o1:" );
foreach ( KeyValuePair<string, string> oKeyValuePair in o1 )
{
    Console.WriteLine( oKeyValuePair.GetHashCode( ) );
}
Console.WriteLine( "o2:" );
foreach ( KeyValuePair<string, string> oKeyValuePair in o2 )
{
    Console.WriteLine( oKeyValuePair.GetHashCode( ) );
}
Console.ReadKey( );

The hash codes for the individual key-value-pairs are the same for both sorted dictionaries, even though the order the values were added differ. This is good.
So I’m missing one step: how do I get ONE unique hash code from all the hash codes?

  • 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-16T06:21:52+00:00Added an answer on May 16, 2026 at 6:21 am

    You will have to be prepared to loop through them all, but there are important short-cuts you can test for first.

    First short-cut, is to check for object identity. While “A is A” isn’t quite as profound as Ayn Rand seems to think, it is a handy way to make equality code faster. Identity always entails equality, and it is common in real code to end up comparing something to itself (esp in collection look-ups, loops and where an object is passed through several layers of code).

    Another is that size cannot be different if contents are the same, and size is fast to obtain.

    Hence the fastest you can get is:

    public static bool EqualSortedDict<K, V>(SortedDictionary<K, V> x, SortedDictionary<K, V> y)
    {
      if(ReferenceEquals(x, y))
        return true;
      if(ReferenceEquals(x, null) || ReferenceEquals(y, null))
        return false; //both being null already hit above.
      if(x.Count != y.Count)
        return false;
      if(!x.Comparer.Equals(y.Comparer))
        return false;//check if this is what you need. Probably is but might
                     //not be in some cases.
      foreach(KeyValuePair<K, V> kvp in x)
      {
        V cmpValue = default(V);
        if(!y.TryGetValue(kvp.Key, out cmpValue) || !kvp.Value.Equals(cmpValue))
          return false;
      }
      return true;
    }
    

    Note that the reason GetHashCode() didn’t work above is that the default implementation of GetHashCode() on SortedDictionary just works on object identity. If you need a value-based GetHashCode() (if your SortedDictionary will itself be a key or placed in a HashSet) then you will have to implement an IEqualityComparer that produces an appropriate hash code. Even then, it can take a long time to compute itself (it will probably always loop through all items to do what you want) and while a.GetHashCode() != b.GetHashCode() proves that a != b (for the definition of equality supported by the hashcode), a.GetHashCode() == b.GetHashCode() does not prove that a == b, as there will be collisions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if there is a way in Spring to have a parent controller:
I was just wondering whether there is some way to do this: I have
I have two ftp servers with fxp enabled on both, I'm just wondering how
I was just wondering, is it possible to have have two sockets that communicate
I have two databases (postgres) that are absolutely the same. Now, I was wondering
I was just wondering when you have for example: var dir = new DirectoryInfo(@C:\Temp);
just wondering what tips people have for avoiding game synchronisation issues in multiplayer games
Just wondering what little scripts/programs people here have written that helps one with his
Just wondering how I can paste an object after I have selected it in
Just wondering weather its faster, if you have lots (~several hundred to a thousand)

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.