Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Remove the specified element from a HashSet in C#
A HashSet in C# is a collection that stores unique elements without any specific order. To remove a specified element from a HashSet, you use the Remove() method, which returns true if the element was found and removed, or false if the element was not present.
Syntax
The Remove() method follows this syntax −
public bool Remove(T item)
Parameters
item − The element to remove from the HashSet.
Return Value
The method returns true if the element was successfully found and removed; otherwise, false.
Using Remove() with String HashSet
The following example demonstrates removing a string element from a HashSet −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet<string> set1 = new HashSet<string>();
set1.Add("AB");
set1.Add("CD");
set1.Add("EF");
set1.Add("AB");
set1.Add("IJ");
set1.Add("KL");
set1.Add("EF");
set1.Add("OP");
Console.WriteLine("Elements in HashSet1");
foreach(string val in set1) {
Console.WriteLine(val);
}
HashSet<string> set2 = new HashSet<string>();
set2.Add("EF");
set2.Add("KL");
Console.WriteLine("Elements in HashSet2");
foreach(string val in set2) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements in HashSet2 = " + set2.Count);
bool removed = set2.Remove("KL");
Console.WriteLine("Was 'KL' removed? " + removed);
Console.WriteLine("\nElements in HashSet2... (UPDATED)");
foreach (string res in set2) {
Console.WriteLine(res);
}
Console.WriteLine("Count of elements in HashSet2 (Updated) = " + set2.Count);
Console.WriteLine("\nIs set1 a superset of set2? " + set1.IsSupersetOf(set2));
}
}
The output of the above code is −
Elements in HashSet1 AB CD EF IJ KL OP Elements in HashSet2 EF KL Count of elements in HashSet2 = 2 Was 'KL' removed? True Elements in HashSet2... (UPDATED) EF Count of elements in HashSet2 (Updated) = 1 Is set1 a superset of set2? True
Using Remove() with Integer HashSet
Here's an example of removing integer elements from a HashSet −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet<int> set1 = new HashSet<int>();
set1.Add(100);
set1.Add(200);
set1.Add(300);
set1.Add(400);
set1.Add(500);
set1.Add(600);
Console.WriteLine("Elements in HashSet");
foreach(int val in set1) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements in HashSet = " + set1.Count);
bool removed = set1.Remove(300);
Console.WriteLine("Was 300 removed? " + removed);
Console.WriteLine("\nElements in HashSet... (UPDATED)");
foreach (int res in set1) {
Console.WriteLine(res);
}
Console.WriteLine("Count of elements in HashSet (Updated) = " + set1.Count);
}
}
The output of the above code is −
Elements in HashSet 100 200 300 400 500 600 Count of elements in HashSet = 6 Was 300 removed? True Elements in HashSet... (UPDATED) 100 200 400 500 600 Count of elements in HashSet (Updated) = 5
Removing Non-Existent Elements
When attempting to remove an element that doesn't exist in the HashSet, the method returns false −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
HashSet<string> fruits = new HashSet<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Orange");
Console.WriteLine("Original HashSet:");
foreach(string fruit in fruits) {
Console.WriteLine(fruit);
}
bool removed1 = fruits.Remove("Banana");
Console.WriteLine("\nRemoved 'Banana': " + removed1);
bool removed2 = fruits.Remove("Grape");
Console.WriteLine("Removed 'Grape': " + removed2);
Console.WriteLine("\nFinal HashSet:");
foreach(string fruit in fruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Original HashSet: Apple Banana Orange Removed 'Banana': True Removed 'Grape': False Final HashSet: Apple Orange
Conclusion
The Remove() method in C# HashSet efficiently removes a specified element and returns a boolean indicating success. It provides O(1) average time complexity for removal operations, making HashSet ideal for scenarios requiring fast element lookups and removals.
