I need to determine whether or not two sets contains exactly the same elements. The ordering does not matter.
For instance, these two arrays should be considered equal:
IEnumerable data = new []{3, 5, 6, 9};
IEnumerable otherData = new []{6, 5, 9, 3}
One set cannot contain any elements, that are not in the other.
Can this be done using the built-in query operators? And what would be the most efficient way to implement it, considering that the number of elements could range from a few to hundreds?
解决方案
If you want to treat the arrays as "sets" and ignore order and duplicate items, you can use HashSet.SetEquals method:
var isEqual = new HashSet(first).SetEquals(second);
Otherwise, your best bet is probably sorting both sequences in the same way and using SequenceEqual to compare them.