How to check if a HashSet is a subset of a collection in C#
The IsSubsetOf() method
The HashSet<T> generic class in the System.Collections.Generic namespace provides the IsSubsetOf() method, which determines if a HashSet<T> object is a subset of the passed input collection.
The illustration below shows how the IsSubsetOf() method works.
Syntax
public bool IsSubsetOf (System.Collections.Generic.IEnumerable<T> other);
Parameters
The method takes an IEnumerable<T> collection as input.
Return value
IsSubsetOf() returns true if the HashSet is a subset of the input collection, and false otherwise.
Things to note
-
This method also returns
trueif theHashSet<T>object is empty, as an empty set is a subset of any other set, including an empty set. -
IsSubsetOf()returnsfalseif the count of elements inHashSet<T>is greater than the number of elements in the input collection. -
This is an operation, where is the count of elements in the passed collection of elements and is the number of elements in the
HashSet<T>. -
In special cases where the passed input collection is also a
HashSet<T>, with the same equality comparer as the currentHashSet<T>object, it becomes an operation, where is the number of elements in the passed input collection. -
The method throws an exception if the input collection is
null. -
The
IsSubsetOf()method does not change the state of theHashSet<T>.
Code
The code below demonstrates the use of the IsSubsetOf() method.
using System;using System.Collections.Generic;class HashSetSubset{static void Main(){HashSet<int> numSet = new HashSet<int>();numSet.Add(1);numSet.Add(5);numSet.Add(9);Console.WriteLine("HashSet Elements : ");PrintSet(numSet);List<int> otherList = new List<int>{9,11,15,6,5,1};Console.WriteLine("Input Collection 1 : {0}", string.Join(" ", otherList.ToArray()));bool isSubset = numSet.IsSubsetOf(otherList);Console.WriteLine($"Is HashSet a subset of Input Collection 1 ?: {isSubset}");otherList = new List<int>{34,11,23,6,5,1};Console.WriteLine("Input Collection 2 : {0}", string.Join(" ", otherList.ToArray()));isSubset = numSet.IsSubsetOf(otherList);Console.WriteLine($"Is HashSet a subset of Input Collection 2 ? : {isSubset}");}private static void PrintSet(HashSet<int> set){Console.Write("{");foreach (int i in set){Console.Write(" {0}", i);}Console.WriteLine(" }");}}
Explanation
-
In this code example, we create a
HashSetof integers and add a few numbers to it. -
We also create two lists of integers, one with all the
HashSetelements and the other with a few missing elements from theHashSet. -
Mathematically, a subset is a set of which all the elements are contained in another set. Therefore, the first list is a subset of the
HashSet, while the second list is not. -
We call
IsSubsetOf()with the first list of integers as input. Since the first list has all of the elements of theHashSet,IsSubsetOf()returnstrue. -
We call
IsSubsetOf()again with the second list of integers as input. Since the second list does not have all of the elements of the HashSet,IsSubsetOf()returnsfalse. -
We also print the elements of the
HashSetand both lists.