C# program to remove an item from Set

A HashSet in C# is a collection that stores unique elements and provides efficient methods to add, remove, and search items. To remove items from a HashSet, you can use methods like Remove(), RemoveWhere(), or Clear().

Syntax

Following are the common methods to remove items from a HashSet −

// Remove a specific item
bool removed = hashSet.Remove(item);

// Remove items based on a condition
int count = hashSet.RemoveWhere(predicate);

// Remove all items
hashSet.Clear();

Using Remove() Method

The Remove() method removes a specific item from the HashSet and returns true if the item was found and removed, false otherwise −

using System;
using System.Collections.Generic;

public class Program {
   public static void Main() {
      var names = new HashSet<string>();
      names.Add("Tim");
      names.Add("John");
      names.Add("Tom");
      names.Add("Kevin");
      
      Console.WriteLine("Initial Set:");
      foreach(var name in names) {
         Console.WriteLine(name);
      }
      
      bool removed = names.Remove("John");
      Console.WriteLine("\nRemoved 'John': " + removed);
      
      Console.WriteLine("\nSet after removing 'John':");
      foreach(var name in names) {
         Console.WriteLine(name);
      }
   }
}

The output of the above code is −

Initial Set:
Tim
John
Tom
Kevin

Removed 'John': True

Set after removing 'John':
Tim
Tom
Kevin

Using RemoveWhere() Method

The RemoveWhere() method removes all items that match a specified condition and returns the count of removed items −

using System;
using System.Collections.Generic;

public class Program {
   public static void Main() {
      var numbers = new HashSet<int> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      
      Console.WriteLine("Initial Set:");
      foreach(var num in numbers) {
         Console.Write(num + " ");
      }
      
      int removedCount = numbers.RemoveWhere(x => x % 2 == 0);
      Console.WriteLine("<br>\nRemoved " + removedCount + " even numbers");
      
      Console.WriteLine("\nSet after removing even numbers:");
      foreach(var num in numbers) {
         Console.Write(num + " ");
      }
   }
}

The output of the above code is −

Initial Set:
1 2 3 4 5 6 7 8 9 10 

Removed 4 even numbers

Set after removing even numbers:
1 3 5 7 9 

Using Clear() Method

The Clear() method removes all elements from the HashSet −

using System;
using System.Collections.Generic;

public class Program {
   public static void Main() {
      var colors = new HashSet<string> {"Red", "Green", "Blue", "Yellow"};
      
      Console.WriteLine("Initial Set Count: " + colors.Count);
      foreach(var color in colors) {
         Console.WriteLine(color);
      }
      
      colors.Clear();
      Console.WriteLine("\nSet Count after Clear(): " + colors.Count);
   }
}

The output of the above code is −

Initial Set Count: 4
Red
Green
Blue
Yellow

Set Count after Clear(): 0

Comparison of Removal Methods

Method Purpose Return Value
Remove(item) Removes a specific item bool - true if removed, false if not found
RemoveWhere(predicate) Removes items matching a condition int - count of removed items
Clear() Removes all items void - no return value

Conclusion

HashSet in C# provides efficient methods to remove items: Remove() for specific items, RemoveWhere() for conditional removal, and Clear() to empty the entire set. These methods maintain the HashSet's performance characteristics while providing flexible removal options.

Updated on: 2026-03-17T07:04:35+05:30

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements