Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
hashset
communitycreator

How to remove all HashSet elements that satisfy a condition in C#

Surabhi Sharma

The HashSet<T> generic class in the System.Collections.Generic namespaceIn C-sharp, we use namespaces to organize too many classes and handle applications easily. provides the RemoveWhere() method.

We use this method to remove all elements from the HashSet<T> that match a condition defined by the specified Predicate<T>.

Syntax

public int RemoveWhere (Predicate<T> match);

The method above takes a Predicate<T> collectionspecialized classes for data storage and retrieval. as input and defines the conditions of the elements to remove.

The RemoveWhere() method returns the number of elements removed from the HashSet<T> in the form of an integer.

Remove elements from HashSet satisfying a condition

Things to note

  • This method is an O(n) operation, where n is the count of elements in the HashSet<T>.

  • It throws an exception if the input predicate is null.

  • This method changes the state of the HashSet<T>.

Linear time complexity O(n) means that any certain algorithm takes proportionally more time to finish as the input grows.

Code

Consider the following example code for RemoveWhere(), which takes numbers as input and removes some of them from the HashSet.

using System;
using System.Collections.Generic;

class HashSetRemover {
    static void Main() {

        HashSet<int> numSet = new HashSet<int>();

        numSet.Add(1);
        numSet.Add(5);
        numSet.Add(9);
        numSet.Add(11);
        numSet.Add(15);
        numSet.Add(6);

        Console.Write("HashSet Elements : ");
        PrintSet(numSet);
        numSet.RemoveWhere(IsLessThan10);

        Console.Write("HashSet Elements After RemoveWhere() : ");
        PrintSet(numSet);
    }

    private static bool IsLessThan10(int num) {
      return (num < 10);
    }
    
    private static void PrintSet(HashSet<int> set) {
      Console.Write("{");

      foreach (int i in set) {
          Console.Write(" {0}", i);
      }
      Console.WriteLine(" }");
    }
}
Remove elements from HashSet with RemoveWhere()

Explanation

In the example above, we create a HashSet of integers and add a few numbers as input.

We also create a function, IsLessThan10(int), which is the Predicate. It takes a number as input and returns True if it is less than 10.

We call the RemoveWhere() method with the IsLessThan10() predicate, and it removes all elements from the HashSet that match this condition. This means that all numbers (less than 10) are removed from the HashSet.

We print the elements of the HashSet before and after the RemoveWhere() operation. The number of removed elements is also displayed. The program prints the output below and exits.

HashSet Elements : 
{ 1 5 9 11 15 6 }
4 Elments removed from HashSet
HashSet Elements After RemoveWhere() : 
{ 11 15 }

RELATED TAGS

c#
hashset
communitycreator
RELATED COURSES

View all Courses

Keep Exploring