उदाहरण 1: स्ट्रिंग HashSets का इंटरसेक्शन
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet set1 = new HashSet();
set1.Add("AB");
set1.Add("CD");
set1.Add("EF");
set1.Add("AB");
set1.Add("IJ");
set1.Add("KL");
set1.Add("EF");
set1.Add("OP");
Console.WriteLine("HashSet1 में तत्व");
foreach(string val in set1) {
Console.WriteLine(val);
}
HashSet set2 = new HashSet();
set2.Add("EF");
set2.Add("KL");
Console.WriteLine("\nHashSet2 में तत्व");
foreach(string val in set2) {
Console.WriteLine(val);
}
Console.WriteLine("HashSet2 में तत्वों की संख्या = " + set2.Count);
set2.Remove("KL");
Console.WriteLine("\nHashSet2 में तत्व... (अपडेटेड)");
foreach (string res in set2) {
Console.WriteLine(res);
}
Console.WriteLine("HashSet2 में तत्वों की संख्या (अपडेटेड) = " + set2.Count);
Console.WriteLine("\nक्या set1, set2 का सुपरसेट है? " + set1.IsSupersetOf(set2));
set1.IntersectWith(set2);
Console.WriteLine("\nइंटरसेक्शन परिणाम...");
foreach(string str in set1) {
Console.WriteLine(str);
}
}
}
आउटपुट
HashSet1 में तत्व
AB
CD
EF
IJ
KL
OP
HashSet2 में तत्व
EF
KL
HashSet2 में तत्वों की संख्या = 2
HashSet2 में तत्व... (अपडेटेड)
EF
HashSet2 में तत्वों की संख्या (अपडेटेड) = 1
क्या set1, set2 का सुपरसेट है? True
इंटरसेक्शन परिणाम...
EF
उदाहरण 2: पूर्णांक HashSets का इंटरसेक्शन
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet set1 = new HashSet();
set1.Add(30);
set1.Add(60);
set1.Add(70);
set1.Add(80);
set1.Add(100);
set1.Add(125);
Console.WriteLine("HashSet1 में तत्व");
foreach(int val in set1) {
Console.WriteLine(val);
}
HashSet set2 = new HashSet();
set2.Add(30);
set2.Add(60);
set2.Add(70);
set2.Add(80);
set2.Add(100);
set2.Add(125);
Console.WriteLine("\nHashSet2 में तत्व");
foreach(int val in set2) {
Console.WriteLine(val);
}
set1.IntersectWith(set2);
Console.WriteLine("\nइंटरसेक्शन परिणाम...");
foreach(int val in set1) {
Console.WriteLine(val);
}
}
}
आउटपुट
HashSet1 में तत्व
30
60
70
80
100
125
HashSet2 में तत्व
30
60
70
80
100
125
इंटरसेक्शन परिणाम...
30
60
70
80
100
125