C# | Union of two HashSet
Last Updated :
24 Mar, 2025
A HashSet is an unordered collection of 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 HashSets, we can use the method HashSet<T>.UnionWith(IEnumerable<T> other) method to perform the union operation.
Syntax:
void HashSet<T>.UnionWith(IEnumerable<T> other)
- Parameters: Takes a second set in which we can perform the union operation.
- Exception: If the Set is null then this method gives ArgumentNullException
Example 1: Performing Union operation using the method HashSet<int>.UnionWith(IEnumerable<int> other)
C#
using System;
using System.Collections.Generic;
class Geeks
{
public static void Main()
{
// Creating a HashSet of integers
var s1 = new HashSet<int>();
// Creating a HashSet of integers
var s2 = new HashSet<int>();
// Inserting even numbers less than
// equal to 10 in HashSet s1
for (int i = 0; i < 5; i++) {
s1.Add(i * 2);
}
// Inserting odd numbers less than
// equal to 10 in HashSet s2
for (int i = 0; i < 5; i++) {
s2.Add(i * 2 + 1);
}
// Creating a new HashSet that contains the union of both HashSets s1 & s2
var ans = new HashSet<int>(s1);
ans.UnionWith(s2);
// Displaying the HashSets and their union
Console.WriteLine("HashSet s1: " + string.Join(" ", s1));
Console.WriteLine("HashSet s2: " + string.Join(" ", s2));
Console.WriteLine("Union of s1 and s2: " + string.Join(", ", ans));
// Creating and initializing HashSets of integers
var s3 = new HashSet<int> { 0 , 3, 5, 7, 9, 10 };
var s4 = new HashSet<int> { 1, 3, 5, 7, 9 };
var un = new HashSet<int>(s3);
// Using the UnionWith() method to find the union of s3 and s4
un.UnionWith(s4);
// Displaying the HashSets and their union
Console.WriteLine("HashSet s3: " + string.Join(" ", s3));
Console.WriteLine("HashSet s4: " + string.Join(" ", s4));
Console.WriteLine("Union of s3 and s4: " + string.Join(", ", un));
}
}
OutputHashSet s1: 0 2 4 6 8
HashSet s2: 1 3 5 7 9
Union of s1 and s2: 0, 2, 4, 6, 8, 1, 3, 5, 7, 9
HashSet s3: 0 3 5 7 9 10
HashSet s4: 1 3 5 7 9
Union of s3 and s4: 0, 3, 5, 7, 9, 10, 1
Example 2: Performing Union Operations on HashSets of strings.
C#
using System;
using System.Collections.Generic;
class Geeks
{
public static void Main()
{
// Creating and initializing HashSets
HashSet<string> s1 =
new HashSet<string> { "Hello", "GeeksforGeeks", "GeeksforGeeks" };
HashSet<string> s2 =
new HashSet<string> { "You", "are", "the", "best" };
// Displaying the elements of the HashSet s1
Console.WriteLine("Elements of HashSet s1: " + String.Join(" ", s1));
// Displaying the elements of the HashSet s2
Console.WriteLine("Elements of HashSet s2: " + String.Join(" ", s2));
// Creating a new HashSet that contains the union of both mySet1 and mySet2
HashSet<string> unionSet = new HashSet<string>(s1);
unionSet.UnionWith(s2);
// Displaying the elements of the Union set
Console.WriteLine("Elements of the Union set: " + String.Join(" ", unionSet));
}
}
OutputElements of HashSet s1: Hello GeeksforGeeks
Elements of HashSet s2: You are the best
Elements of the Union set: Hello GeeksforGeeks You are the best
Advantages of Using HashSet<int>.UnionWith(IEnumerable<int> other) method
- Efficiency: The Union method is optimized for performance, and can efficiently merge two large sets into one without requiring much processing power or memory. This can be particularly useful when working with large data sets.
- No duplicates: The resulting set after the Union operation will contain unique elements. This is because HashSet<T> does not allow duplicate entries.
- Easy to use: The Union method is very easy to use and requires only two hash sets as input parameters. This makes it easy to use in your code without requiring any complex data processing.
- Maintainability: Using the Union method can make your code more maintainable, as it provides a clear and concise way to merge two sets.
- Flexibility: The Union method can be used with any type of object that can be stored in a HashSet<T>. This means that we can use it to merge sets of integers, strings, objects, or any other data type that may be working within our code.