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

C# में दो HashSet का यूनियन

HashSet<T>.UnionWith(IEnumerable<T>) मेथड वर्तमान HashSet को संशोधित करता है ताकि इसमें वर्तमान सेट और निर्दिष्ट संग्रह दोनों में मौजूद सभी तत्व शामिल हों। HashSet अक्रमित है और डुप्लिकेट की अनुमति नहीं देता।

उदाहरण 1: समान सेट

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        HashSet<int> set1 = new HashSet<int>();
        set1.Add(100);
        set1.Add(200);
        set1.Add(300);
        set1.Add(400);
        set1.Add(500);
        set1.Add(600);
        Console.WriteLine("HashSet1 elements...");
        foreach (int ele in set1)
        {
            Console.WriteLine(ele);
        }
        HashSet<int> set2 = new HashSet<int>();
        set2.Add(100);
        set2.Add(200);
        set2.Add(300);
        set2.Add(400);
        set2.Add(500);
        set2.Add(600);
        Console.WriteLine("HashSet2 elements...");
        foreach (int ele in set2)
        {
            Console.WriteLine(ele);
        }
        Console.WriteLine("Union...");
        set1.UnionWith(set2);
        foreach (int ele in set1)
        {
            Console.WriteLine(ele);
        }
    }
}

आउटपुट

HashSet1 elements...
100
200
300
400
500
600
HashSet2 elements...
100
200
300
400
500
600
Union...
100
200
300
400
500
600

उदाहरण 2: आंशिक ओवरलैप

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        HashSet<int> set1 = new HashSet<int>();
        set1.Add(100);
        set1.Add(200);
        set1.Add(300);
        set1.Add(400);
        set1.Add(500);
        set1.Add(600);
        Console.WriteLine("HashSet1 elements...");
        foreach (int ele in set1)
        {
            Console.WriteLine(ele);
        }
        HashSet<int> set2 = new HashSet<int>();
        set2.Add(100);
        set2.Add(250);
        set2.Add(300);
        Console.WriteLine("HashSet2 elements...");
        foreach (int ele in set2)
        {
            Console.WriteLine(ele);
        }
        Console.WriteLine("Union...");
        set1.UnionWith(set2);
        foreach (int ele in set1)
        {
            Console.WriteLine(ele);
        }
    }
}

आउटपुट

HashSet1 elements...
100
200
300
400
500
600
HashSet2 elements...
100
250
300
Union...
100
200
300
400
500
600
250

UnionWith in-place संशोधन करता है। HashSet क्रम की गारंटी नहीं देता; क्रमबद्ध परिणाम के लिए SortedSet का उपयोग करें। अन्य सेट ऑपरेशन: IntersectWith, ExceptWith, SymmetricExceptWith।

  1. - C# में दो TimeSpan जोड़ना

    C# में दो TimeSpan मानों को जोड़ने के लिए Add() मेथड या + ऑपरेटर का उपयोग किया जाता है। परिणाम एक नया TimeSpan होता है जो दोनों अवधियों का योग दर्शाता है। उदाहरण using System; public class Demo { public static void Main() { // दो TimeSpan बनाएं TimeSpan t1 = TimeSpan.FromM

  1. - C# प्रोग्राम दो डिक्शनरी को मर्ज करने के लिए

    दो डिक्शनरी सेट करें:Dictionary<string, int> dict1 = new Dictionary<string, int>(); dict1.Add(laptop, 1); dict1.Add(desktop, 2); Dictionary<string, int> dict2 = new Dictionary<string, int>(); dict2.Add(desktop, 3); dict2.Add(tablet, 4); dict2.Add(mobile, 5);अब दो डिक्शनरी को

  1. - Java में HashSet

    HashSet AbstractSet को एक्सटेंड करता है और Set इंटरफ़ेस को इम्प्लीमेंट करता है। यह एक कलेक्शन बनाता है जो स्टोरेज के लिए हैश टेबल का उपयोग करता है। एक हैश टेबल हैशिंग नामक तंत्र का उपयोग करके जानकारी संग्रहीत करता है। हैशिंग में, एक कुंजी की सूचनात्मक सामग्री का उपयोग एक अद्वितीय मान निर्धारित करने