Remove specific element from an array in C#
This post will discuss how to remove the specified element from an array in C#.
We know that arrays in C# have fixed size, and we cannot simply remove an element from it. However, we can create a new array containing the desired elements from the original arrays. This post provides an overview of some of the available alternatives to accomplish this.
Remove all occurrences of an element from an array:
1. Enumerable.Where() method (System.Linq)
The System.Linq.Enumerable.Where() method filters a sequence of values based on a predicate. The following code example demonstrates how we can use Where() to remove all occurrences of an element from the array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Linq; public class Example { public static void Main() { int[] array = { 1, 3, 4, 5, 4, 2 }; int item = 4; array = array.Where(e => e != item).ToArray(); Console.WriteLine(String.Join(",", array)); } } /* Output: 1,3,5,2 */ |
2. Array.FindAll() method
Array.FindAll() method returns an array containing all the elements that match the specified predicate. Following is a simple example demonstrating usage of this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Linq; public class Example { public static void Main() { int[] array = { 1, 3, 4, 5, 4, 2 }; int item = 4; array = Array.FindAll(array, i => i != item).ToArray(); Console.WriteLine(String.Join(",", array)); } } /* Output: 1,3,5,2 */ |
3. Enumerable.Except() method (System.Linq)
Another solution is to use the Enumerable.Except() method, which compares two sequences and returns the elements that appear only in the first sequence. This method is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Linq; public class Example { public static void Main() { int[] array = { 1, 3, 4, 5, 4, 2 }; int item = 4; array = array.Except(new int[] { item }).ToArray(); Console.WriteLine(String.Join(",", array)); } } /* Output: 1,3,5,2 */ |
Remove only first occurrence of element from an array:
The following code example demonstrates how we can use Where() to remove only the first occurrence of an element from the array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.Linq; public class Example { public static void Main() { int[] array = { 1, 3, 4, 5, 4, 2 }; int item = 4; int index = Array.IndexOf(array, item); array = array.Where((e, i) => i != index).ToArray(); Console.WriteLine(String.Join(",", array)); } } /* Output: 1,3,5,4,2 */ |
We can also convert the array to a list and call its RemoveAt() method to remove the element’s first occurrence. Then, we convert the list back to the array using the ToArray() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { int[] array = { 1, 3, 4, 5, 4, 2 }; int item = 4; List<int> nums = new List<int>(array); nums.RemoveAt(nums.IndexOf(item)); array = nums.ToArray(); Console.WriteLine(String.Join(",", array)); } } /* Output: 1,3,5,4,2 */ |
That’s all about removing the specified element from an array 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 :)