Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
hashset
superset
communitycreator

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

Surabhi Sharma

The HashSet<T> generic class in the System.Collections.Generic namespaceIn C-sharp, we use namespaces to organize too many classes and handle applications easily. provides the 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 True if the HashSet<T> object is empty, as an empty set is a subset of any other set, including an empty set.

  • It returns False if the count of elements that is HashSet<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 current HashSet<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 when IsSupersetOf() is called.

Determining if a hashset is superset of another collection using IsSupersetOf()

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(" }");
    }
}
Determining if a HashSet is a superset of collection using isSuperset()

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

RELATED TAGS

c#
hashset
superset
communitycreator
RELATED COURSES

View all Courses

Keep Exploring