Remove elements from a HashSet with conditions defined by the predicate in C#

The RemoveWhere method in C# allows you to remove elements from a HashSet based on conditions defined by a predicate function. This method takes a delegate that returns true for elements that should be removed and false for elements that should be kept.

Syntax

Following is the syntax for using RemoveWhere method −

public int RemoveWhere(Predicate<T> match)

Parameters

  • match − A predicate delegate that defines the conditions for removal. Returns true for elements to be removed.

Return Value

Returns an int representing the number of elements that were removed from the HashSet.

Using RemoveWhere with Simple Predicate

The following example demonstrates removing a specific element using a predicate function −

using System;
using System.Collections.Generic;

public class Demo {
   private static bool RemoveElement100(int i) {
      return (i == 100);
   }
   
   public static void Main(String[] args) {
      HashSet<int> list = new HashSet<int>();
      list.Add(100);
      list.Add(300);
      list.Add(400);
      list.Add(500);
      list.Add(600);
      
      Console.WriteLine("HashSet elements...");
      foreach (int i in list) {
         Console.WriteLine(i);
      }
      
      int removedCount = list.RemoveWhere(RemoveElement100);
      Console.WriteLine("\nRemoved " + removedCount + " element(s)");
      Console.WriteLine("HashSet after removing element 100...");
      foreach (int i in list) {
         Console.WriteLine(i);
      }
   }
}

The output of the above code is −

HashSet elements...
100
300
400
500
600

Removed 1 element(s)
HashSet after removing element 100...
300
400
500
600

Using RemoveWhere with Complex Conditions

This example removes all elements divisible by 10 using a more complex predicate −

using System;
using System.Collections.Generic;

public class Demo {
   private static bool IsDivisibleBy10(int i) {
      return ((i % 10) == 0);
   }
   
   public static void Main(String[] args) {
      HashSet<int> list = new HashSet<int>();
      list.Add(100);
      list.Add(355);
      list.Add(400);
      list.Add(555);
      list.Add(600);
      
      Console.WriteLine("HashSet elements...");
      foreach (int i in list) {
         Console.WriteLine(i);
      }
      
      int removedCount = list.RemoveWhere(IsDivisibleBy10);
      Console.WriteLine("\nRemoved " + removedCount + " element(s)");
      Console.WriteLine("HashSet after removing elements divisible by 10...");
      foreach (int i in list) {
         Console.WriteLine(i);
      }
   }
}

The output of the above code is −

HashSet elements...
100
355
400
555
600

Removed 3 element(s)
HashSet after removing elements divisible by 10...
355
555

Using Lambda Expression

You can also use lambda expressions instead of separate predicate methods for more concise code −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(String[] args) {
      HashSet<int> numbers = new HashSet<int> {10, 15, 20, 25, 30, 35};
      
      Console.WriteLine("Original HashSet:");
      foreach (int num in numbers) {
         Console.WriteLine(num);
      }
      
      // Remove even numbers using lambda expression
      int removed = numbers.RemoveWhere(x => x % 2 == 0);
      
      Console.WriteLine("\nRemoved " + removed + " even number(s)");
      Console.WriteLine("HashSet after removing even numbers:");
      foreach (int num in numbers) {
         Console.WriteLine(num);
      }
   }
}

The output of the above code is −

Original HashSet:
10
15
20
25
30
35

Removed 3 even number(s)
HashSet after removing even numbers:
15
25
35

Conclusion

The RemoveWhere method provides an efficient way to conditionally remove elements from a HashSet using predicate functions. It returns the count of removed elements and can work with both named methods and lambda expressions for flexible conditional removal.

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

371 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements