Intersection of two HashSets in C#

The intersection of two HashSets in C# finds the common elements present in both collections. The HashSet<T> class provides the IntersectWith() method to perform this operation efficiently.

Syntax

Following is the syntax for finding the intersection of two HashSets −

hashSet1.IntersectWith(hashSet2);

Parameters

  • other − The collection to compare to the current HashSet.

Return Value

The IntersectWith() method does not return a value. It modifies the current HashSet to contain only the elements that exist in both HashSets.

HashSet Intersection Set1 Set2 A, B C, D E, F Intersection: Common elements (C, D) Only shared elements remain in Set1

Using IntersectWith() with String HashSets

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      HashSet<string> set1 = new HashSet<string>();
      set1.Add("AB");
      set1.Add("CD");
      set1.Add("EF");
      set1.Add("IJ");
      set1.Add("KL");
      set1.Add("OP");
      
      Console.WriteLine("Elements in HashSet1:");
      foreach(string val in set1) {
         Console.WriteLine(val);
      }
      
      HashSet<string> set2 = new HashSet<string>();
      set2.Add("EF");
      set2.Add("KL");
      set2.Add("XY");
      
      Console.WriteLine("\nElements in HashSet2:");
      foreach(string val in set2) {
         Console.WriteLine(val);
      }
      
      set1.IntersectWith(set2);
      Console.WriteLine("\nIntersection result:");
      foreach(string str in set1) {
         Console.WriteLine(str);
      }
   }
}

The output of the above code is −

Elements in HashSet1:
AB
CD
EF
IJ
KL
OP

Elements in HashSet2:
EF
KL
XY

Intersection result:
EF
KL

Using IntersectWith() with Integer HashSets

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      HashSet<int> set1 = new HashSet<int>() { 10, 20, 30, 40, 50 };
      HashSet<int> set2 = new HashSet<int>() { 30, 40, 50, 60, 70 };
      
      Console.WriteLine("Elements in HashSet1:");
      foreach(int val in set1) {
         Console.WriteLine(val);
      }
      
      Console.WriteLine("\nElements in HashSet2:");
      foreach(int val in set2) {
         Console.WriteLine(val);
      }
      
      set1.IntersectWith(set2);
      Console.WriteLine("\nIntersection result:");
      foreach(int val in set1) {
         Console.WriteLine(val);
      }
   }
}

The output of the above code is −

Elements in HashSet1:
10
20
30
40
50

Elements in HashSet2:
30
40
50
60
70

Intersection result:
30
40
50

Empty Intersection

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      HashSet<string> set1 = new HashSet<string>() { "A", "B", "C" };
      HashSet<string> set2 = new HashSet<string>() { "X", "Y", "Z" };
      
      Console.WriteLine("HashSet1 count before intersection: " + set1.Count);
      
      set1.IntersectWith(set2);
      
      Console.WriteLine("HashSet1 count after intersection: " + set1.Count);
      Console.WriteLine("Intersection result: " + (set1.Count == 0 ? "Empty" : "Not Empty"));
   }
}

The output of the above code is −

HashSet1 count before intersection: 3
HashSet1 count after intersection: 0
Intersection result: Empty

Conclusion

The IntersectWith() method modifies the original HashSet to contain only elements that exist in both collections. This method is efficient for finding common elements between two HashSets, and if no common elements exist, the resulting HashSet will be empty.

Updated on: 2026-03-17T07:04:36+05:30

599 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements