Intersection of SortedSet with a collection in C#

The IntersectWith method in C# allows you to find the common elements between a SortedSet and any collection that implements IEnumerable<T>. This method modifies the original SortedSet to contain only elements that exist in both collections.

Syntax

Following is the syntax for the IntersectWith method −

public void IntersectWith(IEnumerable<T> other)

Parameters

  • other − The collection to compare with the current SortedSet.

How It Works

The IntersectWith method performs an in-place intersection operation, meaning it modifies the original SortedSet and removes all elements that are not present in the specified collection. The resulting set maintains the sorted order of elements.

SortedSet Intersection Set1 100 200 300 Set2 200 300 450 800 Intersection: {200, 300} Common elements in both sets Original Set1 is modified

Using IntersectWith with Integer SortedSets

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedSet<int> set1 = new SortedSet<int>();
      set1.Add(100);
      set1.Add(200);
      set1.Add(300);
      
      SortedSet<int> set2 = new SortedSet<int>();
      set2.Add(450);
      set2.Add(200);
      set2.Add(650);
      set2.Add(300);
      set2.Add(800);
      
      Console.WriteLine("Set1 before intersection:");
      foreach(int val in set1){
         Console.WriteLine(val);
      }
      
      Console.WriteLine("Does it contain the same elements? = " + set1.SetEquals(set2));
      
      set1.IntersectWith(set2);
      Console.WriteLine("Resultant SortedSet after intersection:");
      foreach(int val in set1){
         Console.WriteLine(val);
      }
   }
}

The output of the above code is −

Set1 before intersection:
100
200
300
Does it contain the same elements? = False
Resultant SortedSet after intersection:
200
300

Using IntersectWith with String SortedSets

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedSet<string> set1 = new SortedSet<string>();
      set1.Add("AB");
      set1.Add("BC");
      set1.Add("CD");
      set1.Add("EF");
      set1.Add("PQ");
      set1.Add("ST");
      
      Console.WriteLine("Elements in SortedSet1:");
      foreach (string res in set1){
         Console.WriteLine(res);
      }
      Console.WriteLine("Count of elements in SortedSet1 = " + set1.Count);
      
      SortedSet<string> set2 = new SortedSet<string>();
      set2.Add("BC");
      set2.Add("CD");
      set2.Add("DE");
      set2.Add("EF");
      set2.Add("AB");
      set2.Add("HI");
      set2.Add("JK");
      
      Console.WriteLine("Elements in SortedSet2:");
      foreach (string res in set2){
         Console.WriteLine(res);
      }
      Console.WriteLine("Count of elements in SortedSet2 = " + set2.Count);
      
      set1.IntersectWith(set2);
      Console.WriteLine("Resultant SortedSet after intersection:");
      foreach(string val in set1){
         Console.WriteLine(val);
      }
   }
}

The output of the above code is −

Elements in SortedSet1:
AB
BC
CD
EF
PQ
ST
Count of elements in SortedSet1 = 6
Elements in SortedSet2:
AB
BC
CD
DE
EF
HI
JK
Count of elements in SortedSet2 = 7
Resultant SortedSet after intersection:
AB
BC
CD
EF

Using IntersectWith with Different Collection Types

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedSet<int> sortedSet = new SortedSet<int>{10, 20, 30, 40, 50};
      List<int> list = new List<int>{20, 40, 60, 80};
      
      Console.WriteLine("Original SortedSet:");
      foreach(int val in sortedSet){
         Console.WriteLine(val);
      }
      
      Console.WriteLine("List elements:");
      foreach(int val in list){
         Console.WriteLine(val);
      }
      
      sortedSet.IntersectWith(list);
      Console.WriteLine("SortedSet after intersection with List:");
      foreach(int val in sortedSet){
         Console.WriteLine(val);
      }
   }
}

The output of the above code is −

Original SortedSet:
10
20
30
40
50
List elements:
20
40
60
80
SortedSet after intersection with List:
20
40

Conclusion

The IntersectWith method provides an efficient way to find common elements between a SortedSet and any enumerable collection. It modifies the original set in-place and maintains the sorted order of the remaining elements, making it ideal for set operations in mathematical computations.

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

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements