Check if the SortedSet contains a specific element in C#

The SortedSet<T> class in C# provides the Contains() method to check if a specific element exists in the sorted set. This method returns true if the element is found, otherwise false.

Syntax

Following is the syntax for the Contains() method −

bool Contains(T item)

Parameters

  • item − The element to locate in the SortedSet.

Return Value

Returns true if the SortedSet contains the specified element; otherwise, false.

Using Contains() with String Elements

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedSet<string> set1 = new SortedSet<string>();
      set1.Add("CD");
      set1.Add("CD");
      set1.Add("CD");
      set1.Add("CD");
      Console.WriteLine("Elements in SortedSet1...");
      foreach (string res in set1){
         Console.WriteLine(res);
      }
      Console.WriteLine("Does the SortedSet1 contains the element DE? = "+set1.Contains("DE"));
      
      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("Does the SortedSet2 contains the element CD? = "+set2.Contains("CD"));
   }
}

The output of the above code is −

Elements in SortedSet1...
CD
Does the SortedSet1 contains the element DE? = False
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
Does the SortedSet2 contains the element CD? = True

Using Contains() with Numeric Elements

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedSet<int> mySet = new SortedSet<int>();
      mySet.Add(100);
      mySet.Add(200);
      mySet.Add(300);
      mySet.Add(400);
      Console.WriteLine("Elements in SortedSet...");
      foreach (int res in mySet){
         Console.WriteLine(res);
      }
      Console.WriteLine("Does the SortedSet contains the element 400? = "+mySet.Contains(400));
      Console.WriteLine("Does the SortedSet contains the element 500? = "+mySet.Contains(500));
   }
}

The output of the above code is −

Elements in SortedSet...
100
200
300
400
Does the SortedSet contains the element 400? = True
Does the SortedSet contains the element 500? = False

Using Contains() for Conditional Operations

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(){
      SortedSet<string> fruits = new SortedSet<string>();
      fruits.Add("Apple");
      fruits.Add("Banana");
      fruits.Add("Orange");
      fruits.Add("Mango");
      
      string searchFruit = "Banana";
      
      if (fruits.Contains(searchFruit)) {
         Console.WriteLine(searchFruit + " is available in the fruit set.");
      } else {
         Console.WriteLine(searchFruit + " is not available. Adding it now.");
         fruits.Add(searchFruit);
      }
      
      Console.WriteLine("Final fruit set:");
      foreach (string fruit in fruits) {
         Console.WriteLine(fruit);
      }
   }
}

The output of the above code is −

Banana is available in the fruit set.
Final fruit set:
Apple
Banana
Mango
Orange

Conclusion

The Contains() method provides an efficient way to check for element existence in a SortedSet with O(log n) time complexity. It's commonly used in conditional logic to determine whether to add, remove, or process elements based on their presence in the set.

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

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements