Remove all occurrences of an item from a List in C#
This post will discuss how to remove all occurrences of an item from a List in C#.
1. Using RemoveAll() Method
The List<T>.RemoveAll() method is often used to remove all the elements matching the specified predicate. The predicate should define the conditions of the elements to remove. The following example demonstrates this by removing all occurrences of element 2 from the list:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> values = new List<int>() { 1, 2, 3, 4, 2, 5 }; values.RemoveAll(item => item == 2); Console.WriteLine(String.Join(", ", values)); // 1, 3, 4, 5 } } |
Since the RemoveAll() method accepts a predicate, it can be used to conditionally remove elements. For example, the following code creates a list of objects, and then removes all objects with the founded field having a value more than 1920.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Linq; using System.Collections.Generic; public class Company { public string name; public int founded; public Company(string name, int founded) { this.name = name; this.founded = founded; } public override string ToString() { return "[" + name + ", " + founded + "]"; } } public class Example { public static void Main() { List<Company> cars = new List<Company>(); cars.Add(new Company("Ford", 1903)); cars.Add(new Company("Chevrolet", 1908)); cars.Add(new Company("Toyota", 1937)); cars.Add(new Company("Honda", 1948)); cars.RemoveAll(s => s.founded > 1920); Console.WriteLine(String.Join(", ", cars)); } } |
Output:
[Ford, 1903], [Chevrolet, 1908]
2. Using Remove() Method
The List.Remove() method removes the first occurrence of a specific object from the List and returns true on successful removal. Although highly inefficient, you can make use of the return value of the Remove() method to repeatedly remove an item from the list until all its occurrences are removed, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> values = new List<int>() { 1, 2, 3, 4, 2, 5 }; while (values.Contains(2)) { values.Remove(2); } Console.WriteLine(String.Join(", ", values)); // 1, 3, 4, 5 } } |
3. Using Where() Method
If you need a new list with the desired elements removed without touching the original list, you can use LINQ. The Where() method filters a sequence of values based on a predicate. For example, the following code returns all items except the one having a value of 2.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> values = new List<int>() { 1, 2, 3, 4, 2, 5 }; values = values.Where(item => item != 2).ToList(); Console.WriteLine(String.Join(", ", values)); // 1, 3, 4, 5 } } |
That’s all about removing all occurrences of an item from a List in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)