A HashSet is an unordered collection of the unique elements. It is found in 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. HashSet
Syntax:
mySet2.ExceptWith(mySet1)
Here, mySet1 and mySet2 are the two HashSets and the function returns the elements of mySet2 which are not in mySet1.
Exception: This method will give ArgumentNullException if the HashSet is null.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to remove all elements // in a collection from a 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>(); // Inserting elements into HashSet mySet1 for (int i = 0; i < 7; i++) { mySet1.Add(i); } // Creating a HashSet of integers HashSet<int> mySet2 = new HashSet<int>(); // Inserting elements into HashSet mySet2 for (int i = 4; i < 11; i++) { mySet2.Add(i); } // Removing all elements in a collection from a HashSet mySet2.ExceptWith(mySet1); // Printing the elements in mySet2 // It only prints the elements which are // in mySet2 and not in mySet1 foreach(int i in mySet2) { Console.WriteLine(i); } } } |
7 8 9 10
Example 2:
// C# code to remove all elements // in a collection from a 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>(); // Inserting elements into HashSet mySet1 mySet1.Add("Punjab"); mySet1.Add("Haryana"); mySet1.Add("Jammu"); mySet1.Add("Himachal"); mySet1.Add("Delhi"); // Displaying all elements in mySet1 Console.WriteLine("The elements in mySet1 are : "); foreach(string i in mySet1) { Console.WriteLine(i); } // Creating a HashSet of strings HashSet<string> mySet2 = new HashSet<string>(); // Inserting elements into HashSet mySet2 mySet2.Add("Bangalore"); mySet2.Add("Kerala"); mySet2.Add("Uttar Pradesh"); mySet2.Add("Himachal"); mySet2.Add("Delhi"); // Displaying all elements in mySet2 Console.WriteLine("The elements in mySet2 are : "); foreach(string i in mySet2) { Console.WriteLine(i); } // Removing all elements in a collection from a HashSet mySet2.ExceptWith(mySet1); // Printing the elements in mySet2 // It only prints the elements which are // in mySet2 and not in mySet1 Console.WriteLine("The elements in mySet2 are : "); foreach(string i in mySet2) { Console.WriteLine(i); } } } |
The elements in mySet1 are : Punjab Haryana Jammu Himachal Delhi The elements in mySet2 are : Bangalore Kerala Uttar Pradesh Himachal Delhi The elements in mySet2 are : Bangalore Kerala Uttar Pradesh
Reference:
Recommended Posts:
- C# | Check if HashSet and the specified collection contain the same elements
- C# | Check if a HashSet and a specified collection share common elements
- C# | Check if a HashSet is a proper superset of the specified collection
- C# | Check if a HashSet is a subset of the specified collection
- C# | Check if a HashSet is a proper subset of the specified collection
- C# | Check if a HashSet is a superset of the specified collection
- C# | Create HashSet from another collection
- C# | Remove all elements from a HashSet
- C# | Remove elements from a HashSet with conditions defined by the predicate
- C# | Remove all elements from the Collection<T>
- C# | Remove the specified element from a HashSet
- C# | Number of elements in HashSet
- C# | Remove element at specified index of Collection<T>
- C# | Copy the elements of collection over a range of elements in ArrayList
- C# | Add element to HashSet
- C# | Union of two HashSet
- C# | Check if a HashSet contains the specified element
- C# | Check if two HashSet<T> objects are equal
- C# | HashSet Class
- C# | Getting an enumerator that iterates through HashSet<T>
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.

