VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-remove-the-specified-element-from-a-hashset/

⇱ C# | Remove the specified element from a HashSet - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# | Remove the specified element from a HashSet

Last Updated : 11 Jul, 2025

A HashSet is an unordered collection of the unique elements. It comes under 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<T>.Remove(T) method is used to remove the specified element from a HashSet<T> object. 

Syntax:

public bool Remove (T item);

Here, item is the element which is to be removed. 

Return Value: The method returns True if the element is successfully found and removed and returns False if item is not found in the HashSet<T> object. 

Below examples illustrate the use of HashSet.Remove(T) Method

Example 1: 

Output:
The elements in HashSet are : 
0
2
4
6
8
10
12
14
16
18
Number of elements are : 10
The elements in HashSet are : 
0
2
4
6
8
12
14
16
18
Number of elements are : 9

Example 2: 

Output:
The elements in HashSet are : 
Data Structures
Algorithms
Java
Puzzles
Coding
Number of elements are : 5
The elements in HashSet are : 
Data Structures
Algorithms
Java
Puzzles
Coding
Number of elements are : 5

Reference:

Comment

Explore