Open In App

C# | Remove the specified element from a HashSet

Improve
Improve
Like Article
Like
Save
Share
Report

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: 

CSHARP




// C# code to remove the specified
// element from a HashSet
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Driver code
    public static void Main()
    {
 
        // Creating a HashSet of integers
        HashSet<int> mySet = new HashSet<int>();
 
        // Inserting even numbers less than
        // equal to 20 in HashSet mySet
        for (int i = 0; i < 10; i++) {
            mySet.Add(i * 2);
        }
 
        Console.WriteLine("The elements in HashSet are : ");
 
        // Displaying the elements in HashSet
        foreach(int i in mySet)
        {
            Console.WriteLine(i);
        }
 
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
 
        // Removing the element 10 if present
        if (mySet.Contains(10)) {
            mySet.Remove(10);
        }
 
        Console.WriteLine("The elements in HashSet are : ");
 
        // Displaying the elements in HashSet
        foreach(int i in mySet)
        {
            Console.WriteLine(i);
        }
 
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
    }
}


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: 

CSHARP




// C# code to remove the specified
//  element from a HashSet
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Driver code
    public static void Main()
    {
 
        // Creating a HashSet of strings
        HashSet<string> mySet = new HashSet<string>();
 
        // Inserting elements into HashSet
        mySet.Add("Data Structures");
        mySet.Add("Algorithms");
        mySet.Add("Java");
        mySet.Add("Puzzles");
        mySet.Add("Coding");
 
        Console.WriteLine("The elements in HashSet are : ");
 
        // Displaying the elements in HashSet
        foreach(string i in mySet)
        {
            Console.WriteLine(i);
        }
 
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
 
        // Removing the element "JavaScript" if present
        if (mySet.Contains("JavaScript")) {
            mySet.Remove("JavaScript");
        }
 
        Console.WriteLine("The elements in HashSet are : ");
 
        // Displaying the elements in HashSet
        foreach(string i in mySet)
        {
            Console.WriteLine(i);
        }
 
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
    }
}


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:



Last Updated : 02 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads