C# | Union of two HashSet

A HashSet is an unordered collection of the unique elements. It comes under the System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. For the Union of two HashSet, HashSet.UnionWith(IEnumerable) Method is used.

Syntax:

firstSet.UnionWith(secondSet)

Exception: If the Set is null then this method give ArgumentNullException.



Below given are some examples to understand the implementation in a better way:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to find Union of two HashSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HashSet of integers
        HashSet<int> mySet1 = new HashSet<int>();
  
        // Creating a HashSet of integers
        HashSet<int> mySet2 = new HashSet<int>();
  
        // Inserting even numbers less than
        // equal to 10 in HashSet mySet1
        for (int i = 0; i < 5; i++) {
            mySet1.Add(i * 2);
        }
  
        // Inserting odd numbers less than
        // equal to 10 in HashSet mySet2
        for (int i = 0; i < 5; i++) {
            mySet1.Add(i * 2 + 1);
        }
  
        // Creating a new HashSet that contains
        // the union of both the HashSet mySet1 & mySet2
        HashSet<int> ans = new HashSet<int>(mySet1);
  
        ans.UnionWith(mySet2);
  
        // Printing the union of both the HashSet
        foreach(int i in ans)
        {
            Console.WriteLine(i);
        }
    }
}

chevron_right


Output:

0
2
4
6
8
1
3
5
7
9

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to find Union of two HashSet
using System;
using System.Collections.Generic;
   
class GFG {
   
    // Driver code
    public static void Main()
    {
   
        // Creating a HashSet of strings
        HashSet<string> mySet1 = new HashSet<string>();
   
        // Creating a HashSet of strings
        HashSet<string> mySet2 = new HashSet<string>();
   
        // Inserting elements in mySet1
        mySet1.Add("Hello");
        mySet1.Add("GeeksforGeeks");
        mySet1.Add("GeeksforGeeks");
   
        // Inserting elements in mySet2
        mySet2.Add("You");
        mySet2.Add("are");
        mySet2.Add("the");
        mySet2.Add("best");
   
        // Creating a new HashSet that contains
        // the union of both the HashSet mySet1 & mySet2
        HashSet<string> ans = new HashSet<string>(mySet1);
   
        ans.UnionWith(mySet2);
   
        // Printing the union of both the HashSet
        foreach(string i in ans)
        {
            Console.WriteLine(i);
        }
    }
}

chevron_right


Output:

Hello
GeeksforGeeks
You
are
the
best

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.