Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
communitycreator

How to check if a HashSet is a subset of a collection in C#

Surabhi Sharma

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.

Using the IsSubsetOf() method to check if a hashset is a subset of another collection

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 true if the HashSet<T> object is empty, as an empty set is a subset of any other set, including an empty set.

  • IsSubsetOf() returns false if the count of elements in HashSet<T> is greater than the number of elements in the input collection.

  • This is an O(n+m)O(n+m) operation, where nn is the count of elements in the passed collection of elements and mm 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 current HashSet<T> object, it becomes an O(n)O(n) operation, where nn 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 the HashSet<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(" }");
    }
}
Using the IsSubsetOf() method to check if a hashset is a subset of another collection

Explanation

  • In this code example, we create a HashSet of integers and add a few numbers to it.

  • We also create two lists of integers, one with all the HashSet elements and the other with a few missing elements from the HashSet.

  • 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 the HashSet, IsSubsetOf() returns true.

  • 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() returns false.

  • We also print the elements of the HashSet and both lists.

RELATED TAGS

c#
communitycreator
RELATED COURSES

View all Courses

Keep Exploring