C# | Remove the specified element from a HashSet Last Updated : 02 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like 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: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.remove?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Remove the specified element from a HashSet S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-HashSet CSharp-Generic-Namespace +1 More Practice Tags : Misc Similar Reads C# | Removing the specified element from the List List.Remove(T) Method is used to remove the first occurrence of a specific object from the List. Properties of List: It is different from the arrays. A list can be resized dynamically but arrays cannot. List class can accept null as a valid value for reference types and it also allows duplicate elem 2 min read C# | Remove all elements from the Hashtable The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Clear Method is used to remove all elements from the Hashtable. Syntax: myTable.Clear() Here myTable is the name o 2 min read C# | Remove all elements from a HashSet A HashSet is an unordered collection of the unique elements. It comes under the 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. HashS 2 min read C# | Remove the element with the specified key from the Hashtable The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Remove(Object) Method is used to remove the element with the specified key from the Hashtable. Syntax: public virt 2 min read C# | Remove the element with the specified key from a SortedList SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.Remove(Object) method is used to remove the element with the specifi 3 min read Like