Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में SortedSet में तत्वों की संख्या प्राप्त करें


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
  1. - C# में SortedSet से सभी तत्व हटाएं

    उदाहरण SortedSet<T> की Clear() मेथड सेट से सभी तत्वों को हटा देती है। इस उदाहरण में दो SortedSet बनाए गए हैं, दूसरे में तत्व जोड़े गए हैं, फिर Clear() कॉल करके खाली किया गया है। using System; using System.Collections.Generic; public class Demo { public static void Main() { SortedS

  1. - C# में फ़ाइल में बाइट्स की संख्या प्राप्त करना

    FileInfo प्रकार में एक Length प्रॉपर्टी होती है जो निर्धारित करती है कि फ़ाइल में कितने बाइट्स हैं।सबसे पहले, फ़ाइल सेट करें:FileInfo file = new FileInfo("D:\\new");अब Length प्रॉपर्टी का उपयोग करें:file.Lengthयहाँ पूरा कोड है:उदाहरणusing System; using System.Linq; using System.IO; class P

  1. - C# में List.GetRange() से तत्वों की रेंज प्राप्त करना

    C# में List<T>.GetRange() मेथड का उपयोग करके सूची के एक सन्निकट (contiguous) भाग को एक नई सूची के रूप में प्राप्त किया जा सकता है। यह ArraySegment या LINQ के Skip()/Take() के समान है लेकिन सूची-विशिष्ट है। उदाहरण using System; using System.Collections.Generic; public class Demo { public