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 HashSet is a proper superset of the specified collection in C#
The IsProperSupersetOf() method in C# checks if a HashSet is a proper superset of the specified collection. A proper superset means the HashSet contains all elements of the other collection plus at least one additional element.
Syntax
Following is the syntax for using the IsProperSupersetOf() method −
public bool IsProperSupersetOf(IEnumerable<T> other)
Parameters
other − The collection to compare with the current HashSet.
Return Value
Returns true if the HashSet is a proper superset of the specified collection; otherwise, false.
Using IsProperSupersetOf() with Integer HashSet
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet<int> set1 = new HashSet<int>();
set1.Add(30);
set1.Add(60);
set1.Add(70);
set1.Add(80);
set1.Add(100);
set1.Add(125);
set1.Add(150);
set1.Add(200);
Console.WriteLine("Elements in HashSet1");
foreach(int val in set1) {
Console.WriteLine(val);
}
HashSet<int> set2 = new HashSet<int>();
set2.Add(30);
set2.Add(60);
set2.Add(70);
Console.WriteLine("Elements in HashSet2");
foreach(int val in set2) {
Console.WriteLine(val);
}
Console.WriteLine("Is set1 a proper superset of set2? " + set1.IsProperSupersetOf(set2));
}
}
The output of the above code is −
Elements in HashSet1 30 60 70 80 100 125 150 200 Elements in HashSet2 30 60 70 Is set1 a proper superset of set2? True
Using IsProperSupersetOf() with String HashSet
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("GH");
set1.Add("IJ");
set1.Add("KL");
set1.Add("MN");
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");
Console.WriteLine("Elements in HashSet2");
foreach(string val in set2) {
Console.WriteLine(val);
}
Console.WriteLine("Is set1 a proper superset of set2? " + set1.IsProperSupersetOf(set2));
}
}
The output of the above code is −
Elements in HashSet1 AB CD EF GH IJ KL MN OP Elements in HashSet2 EF KL Is set1 a proper superset of set2? True
Edge Cases with IsProperSupersetOf()
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet<int> set1 = new HashSet<int> {1, 2, 3};
HashSet<int> set2 = new HashSet<int> {1, 2, 3};
HashSet<int> set3 = new HashSet<int> {1, 2, 3, 4};
HashSet<int> emptySet = new HashSet<int>();
Console.WriteLine("set1: {1, 2, 3}");
Console.WriteLine("set2: {1, 2, 3}");
Console.WriteLine("set3: {1, 2, 3, 4}");
Console.WriteLine("emptySet: {}");
Console.WriteLine();
Console.WriteLine("set1.IsProperSupersetOf(set2): " + set1.IsProperSupersetOf(set2));
Console.WriteLine("set3.IsProperSupersetOf(set1): " + set3.IsProperSupersetOf(set1));
Console.WriteLine("set1.IsProperSupersetOf(emptySet): " + set1.IsProperSupersetOf(emptySet));
}
}
The output of the above code is −
set1: {1, 2, 3}
set2: {1, 2, 3}
set3: {1, 2, 3, 4}
emptySet: {}
set1.IsProperSupersetOf(set2): False
set3.IsProperSupersetOf(set1): True
set1.IsProperSupersetOf(emptySet): True
Key Rules
Returns
falseif both collections have the same elements (not a proper superset).Returns
trueif the HashSet contains all elements of the other collection plus at least one additional element.Any non-empty HashSet is a proper superset of an empty collection.
Conclusion
The IsProperSupersetOf() method determines if a HashSet contains all elements of another collection plus additional elements. It returns false for identical sets and true when the HashSet has extra elements beyond those in the compared collection.
