Remove elements from a SortedSet that match the predicate in C#

The RemoveWhere method in C# allows you to remove elements from a SortedSet<T> based on a specified condition or predicate. This method evaluates each element against the predicate function and removes all elements that satisfy the condition.

Syntax

Following is the syntax for the RemoveWhere method −

public int RemoveWhere(Predicate<T> match)

Parameters

match: A Predicate<T> delegate that defines the conditions of the elements to remove. The predicate returns true for elements that should be removed.

Return Value

The method returns an int representing the number of elements removed from the SortedSet<T>.

RemoveWhere Process Original SortedSet {200, 215, 310, 500, 600} Predicate i % 10 == 0 Returns true for multiples of 10 Result SortedSet {215} Elements matching predicate (200, 310, 500, 600) are removed Only 215 remains as it doesn't match the condition

Using RemoveWhere with Multiple Elements

Example

using System;
using System.Collections.Generic;

public class Demo {
    private static bool IsMultipleOfTen(int i) {
        return ((i % 10) == 0);
    }
    
    public static void Main(String[] args) {
        SortedSet<int> set1 = new SortedSet<int>();
        set1.Add(200);
        set1.Add(215);
        set1.Add(310);
        set1.Add(500);
        set1.Add(600);
        
        Console.WriteLine("SortedSet elements...");
        foreach (int i in set1) {
            Console.WriteLine(i);
        }
        
        Console.WriteLine();
        int removedCount = set1.RemoveWhere(IsMultipleOfTen);
        Console.WriteLine($"Removed {removedCount} elements");
        
        Console.WriteLine("SortedSet after removing multiples of 10...");
        foreach (int i in set1) {
            Console.WriteLine(i);
        }
    }
}

The output of the above code is −

SortedSet elements...
200
215
310
500
600

Removed 4 elements
SortedSet after removing multiples of 10...
215

Using RemoveWhere with Specific Value

Example

using System;
using System.Collections.Generic;

public class Demo {
    private static bool IsSpecificValue(int i) {
        return (i == 500);
    }
    
    public static void Main(String[] args) {
        SortedSet<int> set1 = new SortedSet<int>();
        set1.Add(200);
        set1.Add(215);
        set1.Add(310);
        set1.Add(500);
        set1.Add(600);
        
        Console.WriteLine("SortedSet elements...");
        foreach (int i in set1) {
            Console.WriteLine(i);
        }
        
        Console.WriteLine();
        int removedCount = set1.RemoveWhere(IsSpecificValue);
        Console.WriteLine($"Removed {removedCount} elements");
        
        Console.WriteLine("SortedSet after removing element 500...");
        foreach (int i in set1) {
            Console.WriteLine(i);
        }
    }
}

The output of the above code is −

SortedSet elements...
200
215
310
500
600

Removed 1 elements
SortedSet after removing element 500...
200
215
310
600

Using Lambda Expression

Example

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(String[] args) {
        SortedSet<int> set1 = new SortedSet<int>();
        set1.Add(100);
        set1.Add(150);
        set1.Add(200);
        set1.Add(250);
        set1.Add(300);
        
        Console.WriteLine("Original SortedSet elements...");
        foreach (int i in set1) {
            Console.WriteLine(i);
        }
        
        Console.WriteLine();
        int removedCount = set1.RemoveWhere(x => x > 200);
        Console.WriteLine($"Removed {removedCount} elements greater than 200");
        
        Console.WriteLine("SortedSet after removal...");
        foreach (int i in set1) {
            Console.WriteLine(i);
        }
    }
}

The output of the above code is −

Original SortedSet elements...
100
150
200
250
300

Removed 2 elements greater than 200
SortedSet after removal...
100
150
200

Conclusion

The RemoveWhere method provides an efficient way to conditionally remove multiple elements from a SortedSet<T> based on a predicate function. It returns the count of removed elements and maintains the sorted order of the remaining elements in the collection.

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

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements