using System.Collections.Generic;
public static class Program
public static void Main()
IList<int> items = new List<int>() { 1, 2, 3, 4, 5 };
IEnumerable<int> itemsToDelete = new int[] { 2, 5 };
Console.WriteLine($"Original: [{String.Join(", ", items)}]");
Console.WriteLine($"ToDelete: [{String.Join(", ", itemsToDelete)}]");
HashSet<int> itemsToDeleteSet = new(itemsToDelete);
items.RemoveAll((x, _) => itemsToDeleteSet.Contains(x));
Console.WriteLine($"After RemoveAll: [{String.Join(", ", items)}]");
public static int RemoveAll<T>(this IList<T> list, Func<T, int, bool> predicate)
ArgumentNullException.ThrowIfNull(list);
ArgumentNullException.ThrowIfNull(predicate);
for (; i < list.Count; i++)
if (predicate(list[i], i)) continue;
if (j < i) list[j] = list[i];
for (; i < list.Count; i++, j++)
list.RemoveAt(list.Count - 1);