Skip to content

Instantly share code, notes, and snippets.

@josheinstein
Created May 1, 2012 21:16
Show Gist options
  • Save josheinstein/2571458 to your computer and use it in GitHub Desktop.
Save josheinstein/2571458 to your computer and use it in GitHub Desktop.
Testing sets for equality (IEnumerable and IDictionary)
using System;
using System.Collections;
public static class CollectionHelper {
public static bool SetsEqual(IEnumerable set1, IEnumerable set2) {
var h1 = new Hashtable();
foreach ( var item in set1 ) {
h1[item] = true;
}
foreach ( var item in set2 ) {
if ( !h1.Contains(item)) {
return false;
}
}
return true;
}
public static bool SetsEqual(IDictionary h1, IDictionary h2) {
if (h1.Count != h2.Count) {
return false;
}
foreach ( object key in h1.Keys ) {
if (!Object.Equals(h1[key], h2[key])) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment