42

I want to compare the contents of two Dictionary<string, string> instances regardless of the order of the items they contain. SequenceEquals also compares the order, so I first order the dictionaries by key and then call SequenceEquals.

Is there a method that I can use instead of SequenceEquals that will only compare the contents?

If there isn't, is this the ideal way to do this?

Dictionary<string, string> source = new Dictionary<string, string>();
Dictionary<string, string> target = new Dictionary<string, string>();

source["foo"] = "bar";
source["baz"] = "zed";
source["blah"] = null;

target["baz"] = "zed";
target["blah"] = null;
target["foo"] = "bar";

// sequenceEquals will be false
var sequenceEqual = source.SequenceEqual(target);
// contentsEqual will be true
var contentsEqual = source.OrderBy(x => x.Key).SequenceEqual(target.OrderBy(x => x.Key));
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
  • 2
    The question contains a very big flaw. There is no such thing as an order in which elements are in a dictionary. By definition, a dictionary holds key to value pairs without any implicit ordering. – Zordid Nov 10 '17 at 14:58

4 Answers4

59
var contentsEqual = source.DictionaryEqual(target);

// ...

public static bool DictionaryEqual<TKey, TValue>(
    this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second)
{
    return first.DictionaryEqual(second, null);
}

public static bool DictionaryEqual<TKey, TValue>(
    this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second,
    IEqualityComparer<TValue> valueComparer)
{
    if (first == second) return true;
    if ((first == null) || (second == null)) return false;
    if (first.Count != second.Count) return false;

    valueComparer = valueComparer ?? EqualityComparer<TValue>.Default;

    foreach (var kvp in first)
    {
        TValue secondValue;
        if (!second.TryGetValue(kvp.Key, out secondValue)) return false;
        if (!valueComparer.Equals(kvp.Value, secondValue)) return false;
    }
    return true;
}
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • By having unordered dictionaries, your algorithm runs in O(n^2). – Yuriy Faktorovich Oct 13 '10 at 23:26
  • 3
    @Yuriy: How is it O(n^2), assuming that the hashcodes are half-decent? – LukeH Oct 13 '10 at 23:28
  • Assuming GetHash was implemented. – Yuriy Faktorovich Oct 13 '10 at 23:29
  • 1
    I'll concede, I didn't know GetHashCode implementation on Object returned real hash codes. – Yuriy Faktorovich Oct 13 '10 at 23:35
  • 13
    @Yuriy: If you're using dictionaries then you *either* assume that the hashcodes are half-decent, *or* you make sure that they are. A key lookup in a dictionary is assumed to be roughly O(1). If it's not then you're going to see poor performance all-round, not just in my method. – LukeH Oct 13 '10 at 23:38
  • Should this work for dictionaries of any type? I'm using a `Dictionary` and it's always returning false even though all the values match. – sab669 Jul 15 '15 at 12:19
  • @sab669: Yes, it should work for dictionaries of any type. I suspect that your `string[]` values are treated as unequal because arrays default to reference comparison rather than a comparison of the array contents. You'll need to pass in an `EqualityComparer<>` that compares the contents of the arrays. – LukeH Jul 15 '15 at 12:25
  • It's an useful extension,there is a suggest for that the method also need `IEqualityComparer keyComparer` – Sky Fang Jul 15 '15 at 13:30
6

I don't know if there is an existing method but you could use the following (null checking of args omitted for brevity)

public static bool DictionaryEquals<TKey,TValue>(
  this Dictionary<TKey,TValue> left,
  Dictionary<TKey,TValue> right ) { 

  var comp = EqualityComparer<TValue>.Default;
  if ( left.Count != right.Count ) { 
    return false;
  }
  foreach ( var pair in left ) {
    TValue value;
    if ( !right.TryGetValue(pair.Key, out value) 
         || !comp.Equals(pair.Value, value) ) {
      return false;
    }
  } 
  return true;
}

It would be best to add an overload to allow customization of the EqualityComparer<TValue>.

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

If you use a SortedDictionary you won't need to apply the sorting yourself, which can be slightly easier to use:

void Main()
{
    var d1 = new Dictionary<string, string>
    {
        ["a"] = "Hi there!",
        ["b"] = "asd",
        ["c"] = "def"
    };
    var d2 = new Dictionary<string, string>
    {
        ["b"] = "asd",
        ["a"] = "Hi there!",
        ["c"] = "def"
    };

    var sortedDictionary1 = new SortedDictionary<string, string>(d1);
    var sortedDictionary2 = new SortedDictionary<string, string>(d2);

    if (sortedDictionary1.SequenceEqual(sortedDictionary2))
    {
        Console.WriteLine("Match!");
    }
    else
    {
        Console.WriteLine("Not match!");
    }
}
RB.
  • 36,301
  • 12
  • 91
  • 131
-2

This will check if all Values from source exists in target, ignoring the Keys

var result = source.All(x => target.Any(y => x.Value == y.Value));
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • 4
    This misses the case where `target` has additional key value pairs not in `source` – JaredPar Oct 13 '10 at 23:28
  • @JaredPar: If `target` has additional pairs then what should happen? Return `false` instead of `true`? Then an additional check for length would fix it right? – BrunoLM Oct 13 '10 at 23:34
  • 1
    I would say if the contents are different then they are not equal. A length check would fix the `target` is bigger but not the issue of keys being different. – JaredPar Oct 13 '10 at 23:41
  • 1
    @JaredPar: I assume the OP want to compare just the contents. `[...] that will only compare the contents?` – BrunoLM Oct 13 '10 at 23:43