C# में SortedSet<T> वर्ग की Add(T) विधि का उपयोग करके तत्व जोड़े जा सकते हैं। यह संग्रह तत्वों को स्वचालित रूप से क्रमबद्ध क्रम में रखता है और डुप्लिकेट मानों को अनदेखा करता है।
उदाहरण 1: पूर्णांक SortedSet में तत्व जोड़ना
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet set1 = new SortedSet();
set1.Add(100);
set1.Add(200);
set1.Add(300);
set1.Add(400);
Console.WriteLine("SortedSet में तत्व...");
foreach (int res in set1) {
Console.WriteLine(res);
}
Console.WriteLine("क्या SortedSet में तत्व 500 है? = " + set1.Contains(500));
}
}
आउटपुट
SortedSet में तत्व... 100 200 300 400 क्या SortedSet में तत्व 500 है? = False
उदाहरण 2: डुप्लिकेट के साथ स्ट्रिंग SortedSet
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet set1 = new SortedSet();
set1.Add("CD");
set1.Add("CD");
set1.Add("CD");
set1.Add("CD");
Console.WriteLine("SortedSet1 में तत्व...");
foreach (string res in set1) {
Console.WriteLine(res);
}
SortedSet set2 = new SortedSet();
set2.Add("BC");
set2.Add("CD");
set2.Add("DE");
set2.Add("EF");
set2.Add("AB");
set2.Add("HI");
set2.Add("JK");
Console.WriteLine("SortedSet2 में तत्व...");
foreach (string res in set2) {
Console.WriteLine(res);
}
Console.WriteLine("क्या SortedSet2, SortedSet1 का सुपरसेट है? = " + set2.IsSupersetOf(set1));
}
}
आउटपुट
SortedSet1 में तत्व... CD SortedSet2 में तत्व... AB BC CD DE EF HI JK क्या SortedSet2, SortedSet1 का सुपरसेट है? = True
मुख्य बिंदु
SortedSet<T>तत्वों को स्वचालित क्रमबद्ध क्रम में रखता है- डुप्लिकेट मान स्वचालित रूप से अनदेखे हो जाते हैं
Add()तत्व नया होने परtrue, पहले से मौजूद होने परfalseलौटाता हैContains()से तत्व की उपस्थिति O(log n) में जांच सकते हैं- सेट ऑपरेशन:
UnionWith,IntersectWith,ExceptWith,IsSupersetOfआदि उपलब्ध हैं