Check if a HashSet is a proper superset of the specified collection in C#

CsharpServer Side ProgrammingProgramming

To check if a HashSet is a proper superset of the specified collection, the code is as follows −

Example

 Live Demo

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));
   }
}

Output

This will produce the following output −

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

Example

Let us now see another example −

 Live Demo

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));
   }
}

Output

This will produce the following output −

Elements in HashSet1
AB
CD
EF
GH
IJ
KL
MN
OP
Elements in HashSet2
EF
KL
Is set1 a proper superset of set2? True
raja
Published on 05-Dec-2019 12:16:27
Advertisements