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

C# में SortedSet का किसी संग्रह के साथ इंटरसेक्शन निकालना

उदाहरण 1: पूर्णांक SortedSets का इंटरसेक्शन

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

        SortedSet set2 = new SortedSet();
        set2.Add(450);
        set2.Add(200);
        set2.Add(650);
        set2.Add(300);
        set2.Add(800);

        Console.WriteLine("क्या इसमें समान तत्व हैं? = " + set1.SetEquals(set2));
        set1.IntersectWith(set2);
        Console.WriteLine("परिणामी SortedSet...");
        foreach(int val in set1) {
            Console.WriteLine(val);
        }
    }
}

आउटपुट

क्या इसमें समान तत्व हैं? = False
परिणामी SortedSet...
200
300

उदाहरण 2: स्ट्रिंग SortedSets का इंटरसेक्शन

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedSet set1 = new SortedSet();
        set1.Add("AB");
        set1.Add("BC");
        set1.Add("CD");
        set1.Add("EF");
        set1.Add("PQ");
        set1.Add("ST");

        Console.WriteLine("SortedSet1 में तत्व...");
        foreach (string res in set1) {
            Console.WriteLine(res);
        }
        Console.WriteLine("SortedSet1 में तत्वों की संख्या = " + set1.Count);

        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 में तत्व (SortedSet के लिए एन्यूमरेटर)...");
        SortedSet.Enumerator demoEnum = set2.GetEnumerator();
        while (demoEnum.MoveNext()) {
            string res = demoEnum.Current;
            Console.WriteLine(res);
        }
        Console.WriteLine("SortedSet2 में तत्वों की संख्या = " + set2.Count);

        set1.IntersectWith(set2);
        Console.WriteLine("परिणामी SortedSet...");
        foreach(string val in set1) {
            Console.WriteLine(val);
        }
    }
}

आउटपुट

SortedSet1 में तत्व...
AB
BC
CD
EF
PQ
ST
SortedSet1 में तत्वों की संख्या = 6
SortedSet2 में तत्व (SortedSet के लिए एन्यूमरेटर)...
AB
BC
CD
DE
EF
HI
JK
SortedSet2 में तत्वों की संख्या = 7
परिणामी SortedSet...
AB
BC
CD
EF
  1. - C# में Collection में किसी तत्व की जांच कैसे करें

    C# में Collection<T> किसी विशिष्ट तत्व को रखता है या नहीं, यह जांचने के लिए Contains() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है। उदाहरण 1 using System; using System.Collections.ObjectModel; public class Demo { public static void Main() { Collection col = new Col

  1. - C# में HashSet उचित सबसेट है या नहीं जांचें

    C# में एक HashSet निर्दिष्ट संग्रह का उचित सबसेट है या नहीं, यह जांचने के लिए IsProperSubsetOf() मेथड का उपयोग किया जाता है। यह मेथड true लौटाता है यदि HashSet के सभी तत्व निर्दिष्ट संग्रह में मौजूद हैं और निर्दिष्ट संग्रह में कम से कम एक अतिरिक्त तत्व है। उदाहरण 1 using System; using System.Collect

  1. - C# में संग्रह से तत्व पुनर्प्राप्त करना

    आइए एक लिस्ट संग्रह का उदाहरण देखें।हमने तत्व सेट किए हैं:List<int> list = new List<int>(); list.Add(20); list.Add(40); list.Add(60); list.Add(80);अब मान लें कि हमें लिस्ट से पहला तत्व पुनर्प्राप्त करना है। उसके लिए, इंडेक्स इस तरह सेट करें:int a = list[0];निम्नलिखित एक उदाहरण दिखा रहा ह