How to check if a HashSet is a superset of a collection in C#
The HashSet<T> generic class in the System.Collections.Generic IsSupersetOf() method, which is used to determine if a HashSet<T> object is a superset of a passed input collection.
Syntax
public bool IsSupersetOf (System.Collections.Generic.IEnumerable<T> other);
This method takes an IEnumerable collection as input.
It returns True if the HashSet supersets the input collection and False otherwise.
Things to note
-
This method returns
Trueif theHashSet<T>object is empty, as an empty set is a subset of any other set, including an empty set. -
It returns
Falseif the count of elements that isHashSet<T>is greater than the number of elements in the input collection. -
It is an O(n+m) operation, where n is the count of elements in the passed collection of elements and m is the number of elements in the
HashSet<T>. -
In a special case where the passed input collection is also a
HashSet<T>with the same equality comparer as the currentHashSet<T>object, it becomes an O(n) operation where n is the number of elements in the passed input collection. -
It throws an exception if the input collection is
null. -
The state of the
HashSet<T>does not change whenIsSupersetOf()is called.
Code
In the code example below, we created a HashSet of integers and added a few numbers.
We have also created two lists of integers, one with all its members present in the HashSet and another that has a few elements different from the HashSet.
using System;using System.Collections.Generic;class HashSetSuperset {static void Main() {HashSet<int> numSet = new HashSet<int>();numSet.Add(1);numSet.Add(5);numSet.Add(9);numSet.Add(11);numSet.Add(15);numSet.Add(19);Console.Write("HashSet Elements : ");PrintSet(numSet);List<int> otherList = new List<int>{9,11,15};Console.WriteLine("Input Collection 1 : {0}", string.Join(" ", otherList.ToArray()));bool isSuperset = numSet.IsSupersetOf(otherList);Console.WriteLine($"Is HashSet a superset of Input Collection 1 ?: {isSuperset}");otherList = new List<int>{34,11,23,6,5,1};Console.WriteLine("Input Collection 2 : {0}", string.Join(" ", otherList.ToArray()));isSuperset = numSet.IsSupersetOf(otherList);Console.WriteLine($"Is HashSet a superset of Input Collection 2 ? : {isSuperset}");}private static void PrintSet(HashSet<int> set) {Console.Write("{");foreach (int i in set) {Console.Write(" {0}", i);}Console.WriteLine(" }");}}
Explanation
Mathematically, a superset is a set that contains all the elements of another set. Therefore, the HashSet is a superset of the first list, while it is not the second list.
We call the IsSupersetOf() method with the first list of integers as input. Since the HashSet has all the elements of the first list, IsSupersetOf() returns True and is displayed in the output.
We again call the IsSupersetOf() method with the second list of integers as input. Since it has few different elements from the HashSet, IsSupersetOf() returns False, which is also displayed in the output.
We also print the elements of the HashSet and both lists. The program prints the below output and exits.
HashSet Elements : { 1 5 9 11 15 19 }
Input Collection 1 : 9 11 15
Is HashSet a superset of Input Collection 1 ?: True
Input Collection 2 : 34 11 23 6 5 1
Is HashSet a superset of Input Collection 2 ? : False