Skip to content

Instantly share code, notes, and snippets.

@tathamoddie
Created April 19, 2010 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tathamoddie/371001 to your computer and use it in GitHub Desktop.
Save tathamoddie/371001 to your computer and use it in GitHub Desktop.
Enumerable.SetEqual<T> extension
/// <summary>
/// An order independent version of <see cref="Enumerable.SequenceEqual{TSource}(System.Collections.Generic.IEnumerable{TSource},System.Collections.Generic.IEnumerable{TSource})"/>.
/// </summary>
internal static bool SetEqual<T>(this IEnumerable<T> x, IEnumerable<T> y)
{
if (x == null) throw new ArgumentNullException("x");
if (y == null) throw new ArgumentNullException("y");
var objectsInX = x.ToList();
var objectsInY = y.ToList();
if (objectsInX.Count() != objectsInY.Count())
return false;
foreach (var objectInY in objectsInY)
{
if (!objectsInX.Contains(objectInY))
return false;
objectsInX.Remove(objectInY);
}
return objectsInX.Empty();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment