Check if the SortedSet contains a specific element in C#


To check if the SortedSet contains a specific element, the code is as follows −

Example

 Live Demo

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("SortedSet2 is a superset of SortedSet1? = "+set2.IsSupersetOf(set1));
   }
}

Output

This will produce the following output −

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

Example

Let us see another example -

Let us see another 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));
   }
}

Output

This will produce the following output −

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

Updated on: 04-Dec-2019

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements