Assume I need to compare multiple sets and find which are equals or not.
In the end, I need to compare multiple sets and create a final set, with unique data taken from each set. Also I need to handle all items which are repeating in different sets.
Assume I need to compare multiple sets and find which are equals or not.
In the end, I need to compare multiple sets and create a final set, with unique data taken from each set. Also I need to handle all items which are repeating in different sets.
You should use System.Collections.Generic.HashSet<T>
You can create it like:
var hashSet = new HashSet<T>(IEnumerable<T>)
and then use methods to compare:
hashSet.SetEquals(IEnumerable<T> list) - will return true if hashSet and list contains same items
hashSet.Overlaps(IEnumerable<T> list) - will return true if hashSet contains one of the items from list
hashSet.IsSubsetOf(IEnumerable<T> list) - will return true if hashSet is subset of the list (order don't matter)
hashSet.IsProperSubsetOf(IEnumerable<T> list) - same as IsSubsetOf but order matter
hashSet.IsSupersetOf(IEnumerable<T> list) - will return true if hashSet is superset of the list (order don't matter)
hashSet.IsProperSupersetOf(IEnumerable<T> list) - same as IsSupersetOf but order matter
Also there is methods to modify:
hashSet.UnionWith(IEnumerable<T> list) - will modify hashSet to contain elements which are exists
in current hashSet or list or in both.
hashSet.symmetricExceptWith(IEnumerable<T> list) - will modify hashSet to contain elements which are only exists
in current hashSet or list but not in both.
hashSet.IntersectWith(IEnumerable<T> list) - will modify hashSet to contain elements which are exists
in current hashSet and list.
For more information about HashSet see MSDN article.
You can use the HashSet<T> class, which represents a set of elements with no duplicates, and exposes methods like IntersectWith, UnionWith, ExceptWith etc. for common set operations