C# | Remove all elements in a collection from a HashSet
In C#, you can use the ExceptWith() method to remove all the elements in a collection from a HashSet. The ExceptWith() method removes all the elements in the specified collection from the current HashSet.
Here is an example code that demonstrates how to use the ExceptWith() method to remove all the elements in a collection from a HashSet:
C#
using System;
using System.Collections.Generic;
class Program
{
static void Main( string [] args)
{
HashSet< int > set1 = new HashSet< int > { 1, 2, 3, 4, 5 };
HashSet< int > set2 = new HashSet< int > { 2, 4 };
Console.WriteLine( "Before removing elements from set1:" );
foreach ( int num in set1)
{
Console.Write(num + " " );
}
Console.WriteLine();
set1.ExceptWith(set2);
Console.WriteLine( "After removing elements from set1:" );
foreach ( int num in set1)
{
Console.Write(num + " " );
}
Console.WriteLine();
}
}
|
Output
Before removing elements from set1:
1 2 3 4 5
After removing elements from set1:
1 3 5
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.ExceptWith(IEnumerable) Method is used to remove all elements in the specified collection from the current HashSet object. 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:
CSHARP
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
HashSet< int > mySet1 = new HashSet< int >();
for ( int i = 0; i < 7; i++) {
mySet1.Add(i);
}
HashSet< int > mySet2 = new HashSet< int >();
for ( int i = 4; i < 11; i++) {
mySet2.Add(i);
}
mySet2.ExceptWith(mySet1);
foreach ( int i in mySet2)
{
Console.WriteLine(i);
}
}
}
|
Example 2:
CSHARP
using System;
using System.Collections.Generic;
class GFG {
public static void Main()
{
HashSet< string > mySet1 = new HashSet< string >();
mySet1.Add("Punjab");
mySet1.Add("Haryana");
mySet1.Add("Jammu");
mySet1.Add("Himachal");
mySet1.Add("Delhi");
Console.WriteLine("The elements in mySet1 are : ");
foreach ( string i in mySet1)
{
Console.WriteLine(i);
}
HashSet< string > mySet2 = new HashSet< string >();
mySet2.Add("Bangalore");
mySet2.Add("Kerala");
mySet2.Add("Uttar Pradesh");
mySet2.Add("Himachal");
mySet2.Add("Delhi");
Console.WriteLine("The elements in mySet2 are : ");
foreach ( string i in mySet2)
{
Console.WriteLine(i);
}
mySet2.ExceptWith(mySet1);
Console.WriteLine("The elements in mySet2 are : ");
foreach ( string i in mySet2)
{
Console.WriteLine(i);
}
}
}
|
Reference:
Last Updated :
24 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...