Remove Elements from SortedSet that Match Predicate in Chash



To remove elements from a SortedSet that match the predicate, the code is as follows −

Example

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   private static bool demo(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(" ");
      set1.RemoveWhere(demo);
      Console.WriteLine("SortedSet after removing some elements...");
      foreach (int i in set1) {
         Console.WriteLine(i);
      }
   }
}

Output

This will produce the following output −

SortedSet elements...
200
215
310
500
600
SortedSet after removing some elements...
215

Example

Let us see another example −

 Live Demo

using System;
using System.Collections.Generic;
public class Demo {
   private static bool demo(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(" ");
      set1.RemoveWhere(demo);
      Console.WriteLine("SortedSet after removing an element...");
      foreach (int i in set1) {
         Console.WriteLine(i);
      }
   }
}

Output

This will produce the following output −

SortedSet elements...
200
215
310
500
600
SortedSet after removing an element...
200
215
310
600
Updated on: 2019-12-05T10:38:23+05:30

284 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements