Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
hashset
collection
communitycreator

How to create an intersection of a HashSet 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 IntersectWith() method.

This method modifies the HashSet<T>, such that it contains an intersection of the current HashSet<T> and the specified collectionspecialized classes for data storage and retrieval classes. provided as the input.

Syntax

public void IntersectWith (System.Collections.Generic.IEnumerable<T> other);

The IntersectWith() method takes an IEnumerable collection as input. It also changes the current HashSet<T> object to have only those items that are present in the HashSet<T> and the input collection.

Performing a set intersection operation using IntersectWith() method

Things to note

  • The IntersectWith() method represents the mathematical set intersection operation.

  • It throws an exception if the input collection is null.

  • This method changes the state of the HashSet<T>.

  • It is an O(n + m) operation, where n is the count of elements in the HashSet, and m is the number of elements in the input collection.

  • In a particular case where the input collection is another HashSet<T> collection with the same equality comparer as the current HashSet<T>, this becomes an O(n) operation.

Code

In the example below, we create a HashSet of integers and add a few numbers.

We also create a List of integers with a few elements shared with the HashSet, and some elements not present in the HashSet.

using System;
using System.Collections.Generic;

class HashSetIntersector {
    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(6);

        Console.Write("HashSet Elements : ");
        PrintSet(numSet);

        List<int> otherList = new List<int>{9,11,39};
        Console.WriteLine("Input List 1: {0}", string.Join(" ", otherList.ToArray()));
        numSet.IntersectWith(otherList);

        Console.Write("HashSet Elements after IntersectWith() : ");
        PrintSet(numSet);
    }

    private static void PrintSet(HashSet<int> set) {
      Console.Write("{");

      foreach (int i in set) {
          Console.Write(" {0}", i);
      }
      Console.WriteLine(" }");
    }
}
Create intersection of HashSet with the IntersectWith() method

Explanation

We call the IntersectWith() method with the list of integers as input. It does a set intersection operation and modifies the HashSet to contain the intersection of itself and the specified list of integers.

We also print the elements of the HashSet before and after the IntersectWith() operation, along with the list contents. The program prints the output below and exits.

HashSet Elements : { 1 5 9 11 15 6 }
Input List 1: 9 11 39
HashSet Elements after IntersectWith() : { 9 11 }

RELATED TAGS

c#
hashset
collection
communitycreator
RELATED COURSES

View all Courses

Keep Exploring