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

C# में SortedSet से निर्दिष्ट आइटम हटाएं

उदाहरण

SortedSet<T> की Remove() मेथड निर्दिष्ट तत्व को सेट से हटा देती है यदि वह मौजूद है। यह मेथड एक बूलियन मान लौटाती है जो दर्शाता है कि तत्व सफलतापूर्वक हटाया गया था या नहीं।

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

        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 में तत्व...");
        foreach (string res in set2) {
            Console.WriteLine(res);
        }

        Console.WriteLine("SortedSet में तत्वों की संख्या = " + set2.Count);
        set2.Remove("HI");
        set2.Remove("JK");
        Console.WriteLine("SortedSet में तत्वों की संख्या (अपडेटेड) = " + set2.Count);
    }
}

आउटपुट

SortedSet1 में तत्व...
AB
BC
CD
EF
SortedSet2 में तत्व...
AB
BC
CD
DE
EF
HI
JK
SortedSet में तत्वों की संख्या = 7
SortedSet में तत्वों की संख्या (अपडेटेड) = 5

एक और उदाहरण

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.Remove(100);
        Console.WriteLine("SortedSet में तत्वों की संख्या (अपडेटेड) = " + set1.Count);
    }
}

आउटपुट

SortedSet में तत्व...
100
200
300
400
SortedSet में तत्वों की संख्या = 4
SortedSet में तत्वों की संख्या (अपडेटेड) = 3
  1. - C# प्रोग्राम सेट से आइटम हटाने के लिए

    सबसे पहले, एक HashSet घोषित करें और तत्व जोड़ें: var names = new HashSet(); names.Add(Tim); names.Add(John); names.Add(Tom); names.Add(Kevin); किसी तत्व को हटाने के लिए, RemoveWhere का उपयोग करें। x == John); आइए पूरा उदाहरण देखें: उदाहरण using System; using System.Collections.Generic; public cla

  1. - C# में ArrayList से आइटम कैसे हटाएं

    सबसे पहले, एक नया ArrayList सेट करें और इसमें तत्व जोड़ें। ArrayList arr = new ArrayList(); arr.Add(Jones); arr.Add(Tom); arr.Add(Henry); अब आइटम Tom को हटाएं। इसके लिए, Remove() विधि का उपयोग करें। arr.Remove(Tom); ArrayList से तत्व हटाने का पूरा उदाहरण निम्नलिखित है: उदाहरण using System; using Sy

  1. - C# में इंडेक्स का उपयोग करके लिस्ट से आइटम कैसे हटाएं?

    C# में List<T> से किसी विशिष्ट इंडेक्स पर स्थित एलिमेंट को हटाने के लिए RemoveAt() मेथड का उपयोग किया जाता है। उदाहरण using System; using System.Collections.Generic; class Program { static void Main() { List list1 = new List() { Hanks, Lawrence,