Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if a SortedSet is a superset of the specified collection in C#
To check if a SortedSet is a superset of the specified collection in C#, we use the IsSupersetOf() method. A superset means that one set contains all the elements of another set, and possibly additional elements.
Syntax
Following is the syntax for the IsSupersetOf() method −
public bool IsSupersetOf(IEnumerable<T> other)
Parameters
-
other − The collection to compare with the current SortedSet.
Return Value
Returns true if the SortedSet contains all elements of the specified collection; otherwise, false.
Using IsSupersetOf() with String Collections
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("EF");
Console.WriteLine("Elements in SortedSet1...");
foreach (string res in set1){
Console.WriteLine(res);
}
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));
}
}
The output of the above code is −
Elements in SortedSet1... CD EF Elements in SortedSet2... AB BC CD DE EF HI JK SortedSet2 is a superset of SortedSet1? = True
Using IsSupersetOf() with Integer Collections
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(10);
set1.Add(20);
set1.Add(30);
Console.WriteLine("Elements in SortedSet1...");
foreach (int res in set1){
Console.WriteLine(res);
}
SortedSet<int> set2 = new SortedSet<int>();
set2.Add(10);
set2.Add(20);
set2.Add(30);
set2.Add(40);
set2.Add(50);
set2.Add(60);
Console.WriteLine("Elements in SortedSet2...");
foreach (int res in set2){
Console.WriteLine(res);
}
Console.WriteLine("SortedSet2 is a superset of SortedSet1? = " + set2.IsSupersetOf(set1));
}
}
The output of the above code is −
Elements in SortedSet1... 10 20 30 Elements in SortedSet2... 10 20 30 40 50 60 SortedSet2 is a superset of SortedSet1? = True
Using IsSupersetOf() with Different Collections
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
SortedSet<int> sortedSet = new SortedSet<int>();
sortedSet.Add(1);
sortedSet.Add(2);
sortedSet.Add(3);
sortedSet.Add(4);
sortedSet.Add(5);
List<int> list = new List<int> { 2, 4 };
int[] array = { 1, 3, 5 };
HashSet<int> hashSet = new HashSet<int> { 1, 2, 6 };
Console.WriteLine("SortedSet contains: 1, 2, 3, 4, 5");
Console.WriteLine("List contains: 2, 4");
Console.WriteLine("Array contains: 1, 3, 5");
Console.WriteLine("HashSet contains: 1, 2, 6");
Console.WriteLine();
Console.WriteLine("Is SortedSet superset of List? " + sortedSet.IsSupersetOf(list));
Console.WriteLine("Is SortedSet superset of Array? " + sortedSet.IsSupersetOf(array));
Console.WriteLine("Is SortedSet superset of HashSet? " + sortedSet.IsSupersetOf(hashSet));
}
}
The output of the above code is −
SortedSet contains: 1, 2, 3, 4, 5 List contains: 2, 4 Array contains: 1, 3, 5 HashSet contains: 1, 2, 6 Is SortedSet superset of List? True Is SortedSet superset of Array? True Is SortedSet superset of HashSet? False
Conclusion
The IsSupersetOf() method in C# checks if a SortedSet contains all elements of another collection. It returns true when the SortedSet is a superset, meaning it contains all elements from the specified collection, and false otherwise.
