C# | Union of two HashSet Last Updated : 24 Mar, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 ArgumentNullExceptionExample 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) methodEfficiency: 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. Comment More infoAdvertise with us Next Article C# | Union of two HashSet S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-HashSet CSharp-Generic-Namespace C# +2 More Practice Tags : Misc Similar Reads SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While 7 min read Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br 14 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan 14 min read Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec 14 min read Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android 4 min read Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s 6 min read f-strings in Python Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables directly into strings. The idea behind f-strings is to make 5 min read Python Operators In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /, 6 min read Like