C# | Remove elements from a SortedSet that match the predicate Last Updated : 01 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.RemoveWhere(Predicate<T>) Method is used to remove all elements that match the conditions defined by the specified predicate from a SortedSet<T>. Properties: In C#, SortedSet class can be used to store, remove or view elements. It maintains ascending order and does not store duplicate elements. It is suggested to use SortedSet class if you have to store unique elements and maintain ascending order. Syntax: public int RemoveWhere (Predicate<T> match); Return Value: This method returns the number of elements that were removed from the SortedSet<T> collection. Exception: This method will give ArgumentNullException if the match is null. Note: Calling this method is an O(n) operation, where n is Count i.e, the number of elements that are contained in the SortedSet. Below are the examples to illustrate the SortedSet<T>.RemoveWhere(Predicate<T>) Method Example 1: CSHARP // C# code to remove elements from a SortedSet // that match the predicate using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a SortedSet of integers SortedSet<int> mySet = new SortedSet<int>(); // Inserting elements into SortedSet for (int i = 0; i < 10; i++) { mySet.Add(i); } Console.WriteLine("The elements in SortedSet are : "); // Displaying the elements in SortedSet foreach(int i in mySet) { Console.WriteLine(i); } // Displaying the number of elements in SortedSet Console.WriteLine("Number of elements are : " + mySet.Count); // Remove elements from a SortedSet // with conditions defined by the predicate mySet.RemoveWhere(isEven); Console.WriteLine("The elements in SortedSet are : "); // Displaying the elements in SortedSet foreach(int i in mySet) { Console.WriteLine(i); } // Displaying the number of elements in SortedSet Console.WriteLine("Number of elements are : " + mySet.Count); } // Helper function which tells // whether an element is even or not private static bool isEven(int i) { return ((i % 2) == 0); } } Output: The elements in SortedSet are : 0 1 2 3 4 5 6 7 8 9 Number of elements are : 10 The elements in SortedSet are : 1 3 5 7 9 Number of elements are : 5 Example 2 : CSHARP // C# code to remove elements from a // SortedSet that match the predicate using System; using System.Collections.Generic; class GFG { // Driver code public static void Main() { // Creating a SortedSet of integers SortedSet<int> mySet = new SortedSet<int>(); // Inserting elements into SortedSet for (int i = 0; i < 20; i++) { mySet.Add(i); } // Displaying the number of elements in SortedSet Console.WriteLine("Number of elements are : " + mySet.Count); // Remove elements from a SortedSet // with conditions defined by the predicate mySet.RemoveWhere(myFunc); // Displaying the number of elements in SortedSet Console.WriteLine("Number of elements are : " + mySet.Count); } // Helper function which tells // whether an element is divisible // by both 2 and 3 private static bool myFunc(int i) { return ((i % 2) == 0 && (i % 3 == 0)); } } Output: Number of elements are : 20 Number of elements are : 16 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.sortedset-1.removewhere?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Remove elements from a SortedSet that match the predicate S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Generic-SortedSet CSharp-Generic-Namespace +1 More Practice Tags : Misc Similar Reads C# | Remove all elements from the SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Clear Method is used to remove the all elements from the SortedSet. Properties: In C#, SortedSet class can be used to store, remove or view elements. 2 min read Remove elements from a List that satisfy given predicate in Java Below are the methods to efficiently remove elements from a List satisfying a Predicate condition: p ==> Predicate, specifying the condition l ==> List, from which element to be removed Using iterator Below program demonstrates the removal of null elements from the list, using the Predicate Ja 5 min read C# | Remove the specified element from a HashSet 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 3 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 C# | Remove all elements 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.Clear method is used to remove all the elements from a SortedList ob 2 min read Like