C# में SortedSet<T> में निहित तत्वों की संख्या प्राप्त करने के लिए, हम Count प्रॉपर्टी का उपयोग करते हैं। SortedSet अद्वितीय तत्वों का एक संग्रह है जो स्वचालित रूप से क्रमबद्ध क्रम में बना रहता है। यह System.Collections.Generic नेमस्पेस का हिस्सा है और लाल-काले ट्री पर आधारित है।
उदाहरण 1: दो SortedSet की गिनती
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedSet<string> set1 = new SortedSet<string>();
set1.Add("AB");
set1.Add("BC");
set1.Add("CD");
set1.Add("EF");
Console.WriteLine("SortedSet1 में तत्व...");
foreach (string res in set1)
{
Console.WriteLine(res);
}
Console.WriteLine("SortedSet1 में तत्वों की संख्या = " + set1.Count);
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("SortedSet2 में तत्व... (SortedSet के लिए एन्यूमरेटर)");
SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
while (demoEnum.MoveNext())
{
string res = demoEnum.Current;
Console.WriteLine(res);
}
Console.WriteLine("SortedSet2 में तत्वों की संख्या = " + set2.Count);
}
}
आउटपुट
SortedSet1 में तत्व... AB BC CD EF SortedSet1 में तत्वों की संख्या = 4 SortedSet2 में तत्व... (SortedSet के लिए एन्यूमरेटर) AB BC CD DE EF HI JK SortedSet2 में तत्वों की संख्या = 7
उदाहरण 2: Clear() के बाद गिनती
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedSet<int> set1 = new SortedSet<int>();
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 में तत्वों की संख्या = " + set1.Count);
set1.Clear();
Console.WriteLine("SortedSet में तत्वों की संख्या (अपडेटेड) = " + set1.Count);
}
}
आउटपुट
SortedSet में तत्व... 100 200 300 400 SortedSet में तत्वों की संख्या = 4 SortedSet में तत्वों की संख्या (अपडेटेड) = 0
मुख्य बिंदु:
Countप्रॉपर्टी O(1) समय में तत्वों की संख्या लौटाती हैSortedSetडुप्लिकेट तत्वों को स्वचालित रूप से अस्वीकृत करता है- तत्व हमेशा क्रमबद्ध क्रम में रहते हैं (तुलनाकर्ता के अनुसार)
Clear()सभी तत्वों को हटा देता है और Count को 0 पर सेट करता हैGetEnumerator()क्रमबद्ध क्रम में पुनरावृति करने वाला एन्यूमरेटर लौटाता है- सेट ऑपरेशंस:
UnionWith,IntersectWith,ExceptWith,IsSubsetOf,IsSupersetOf