Remove element from a specific index in C# array
This post will discuss how to remove an element from a specific index in an array in C#.
1. Using Array.Resize method
The idea is to move elements one position to their left, starting from the specified index. Then, decrement the array’s size by one with the Array.Resize() method, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { int[] source = { 5, 4, 7, 2, 9 }; int index = 2; for (int i = index; i < source.Length - 1; i++) { source[i] = source[i + 1]; } Array.Resize(ref source, source.Length - 1); Console.WriteLine(String.Join(", ", source)); // 5, 4, 2, 9 } } |
2. Using Array.Copy Method
Alternatively, you can create a new array with the element at the specified index removed. The Array.Copy method copies a range of elements from an array to another array. It can be used as follows to copy all the elements of the original array to the new array, except the element at the specified index.
|
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 |
using System; public static class Extensions { public static T[] RemoveAt<T>(this T[] source, int index) { T[] dest = new T[source.Length - 1]; if (index > 0) { Array.Copy(source, 0, dest, 0, index); } if (index < source.Length - 1) { Array.Copy(source, index + 1, dest, index, source.Length - index - 1); } return dest; } } public class Example { public static void Main() { int[] source = { 5, 4, 7, 2, 9 }; int[] dest = source.RemoveAt(2); Console.WriteLine(String.Join(", ", dest)); // 5, 4, 2, 9 } } |
3. Using List<T>.RemoveAt Method
Another option is to convert the array into a List and invoke the List<T>RemoveAt() method on it to remove the element at the specified position. Then, transform the list back into a new array with the List.ToArray() method. This translates to the following code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Collections.Generic; public static class Extensions { public static T[] RemoveAt<T>(this T[] source, int index) { var dest = new List<T>(source); dest.RemoveAt(index); return dest.ToArray(); } } public class Example { public static void Main() { int[] source = { 5, 4, 7, 2, 9 }; int[] dest = source.RemoveAt(2); Console.WriteLine(String.Join(", ", dest)); // 5, 4, 2, 9 } } |
4. Using Enumerable.Where Method
The LINQ’s Enumerable.Where() method can be used to filter a sequence based on a predicate that involves the index of each element. The following code example demonstrates its usage to create an array instance with the specified element removed.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; using System.Linq; public static class Extensions { public static T[] RemoveAt<T>(this T[] source, int index) { return source.Where((_, i) => i != index).ToArray(); } } public class Example { public static void Main() { int[] source = { 5, 4, 7, 2, 9 }; int[] dest = source.RemoveAt(2); Console.WriteLine(String.Join(", ", dest)); // 5, 4, 2, 9 } } |
5. Using for loop
Finally, you can write a custom routine for removing an element from the specific index in the array. The following code example creates a new array of one less size and uses a for-loop to fill it with the corresponding values from the original array.
|
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 |
using System; using System.Collections.Generic; public static class Extensions { public static T[] RemoveAt<T>(this T[] source, int index) { T[] dest = new T[source.Length - 1]; for (int i = 0, j = 0; i < source.Length; i++) { if (i != index) { dest[j] = source[i]; j++; } } return dest; } } public class Example { public static void Main() { int[] source = { 5, 4, 7, 2, 9 }; int[] dest = source.RemoveAt(2); Console.WriteLine(String.Join(", ", dest)); // 5, 4, 2, 9 } } |
To be safe, consider placing the null-check and index-out-of-bound check for all the above solutions.
|
1 2 3 4 5 6 7 |
if (source == null) { throw new ArgumentNullException("source"); } if (index < 0 || index >= source.Length) { throw new ArgumentOutOfRangeException("Index " + index + " outside the bounds of source array"); } |
That’s all about removing an element from a specific index in 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 :)