How to Remove Elements from an Array in C#

Arrays are an important data structure in computer programming because they allow us to store a collection of elements of the same data type. Based on specific situations, we may need to remove an element from an array. This article will go over several techniques for How to Remove Elements from an Array in C#.

Install Required Software

  • NET Core SDK from here
  • Visual Studio or Visual Studio Code from here

Introduction Arrays in C#

A collection of elements of the same data type that are stored in memory in a contiguous manner forms an array. The array’s length minus one identifies each element in the array with an index ranging from 0 to the length.

Remove Elements from Array

Array.Copy() method

The Array.Copy() method copies an array’s elements to a new array. This can be used to delete elements from an array by generating a smaller array and copying the elements you want to keep to the new array. The following code, for example, removes the first element from the array myArray and transfers the other elements to a new array:

   
public static int[] arrayCopy(int[] array, int index)
        {
            
            int arrayindex = Array.IndexOf(array, index);   

            int[] newarray= new int[array.Length - 1];
            
            Array.Copy(array,0,newarray,0, arrayindex);
            Array.Copy(array, arrayindex+1, newarray, arrayindex, array.Length - arrayindex - 1);
             
            return newarray;

        }

List<T>.Method RemoveAll()

The List<T>. The RemoveAll() method takes a predicate as a parameter and removes from the list any elements that satisfy the condition. For instance, the code below removes all odd numbers from the array myArray:

 public static int[] arrayremoveAll(int[] array,int index)
        {
            var arrList = new List<int>(array);
            arrList.RemoveAll(arr => arr == index);

            return arrList.ToArray();
        }

RemoveAt() method

The RemoveAt() method takes an index as its argument and removes the element at that index from the array. For example, the following code removes the element at the index from the array myArray.

  public static int[] arrayremoveat(int[] array, int index)
        {
            var arrList = new List<int>(array);
            arrList.RemoveAt(index);

            return arrList.ToArray();
        }

Remove Element from an Array using Linq

Another way to remove an element from an array in C# is to use the Linq library’s Where() method. The idea is to create a new array with all the elements except the one to be removed using a lambda expression that excludes the element based on certain conditions.

public static int[] arrayremoveLinq(int[] array, int index)
        {
             int[] newarray = array.Where(arr => arr != index).ToArray(); 
            return newarray;

        }

Removing Multiple Elements from an Array

To remove multiple elements from an array in C#, we can use similar approaches as we used for removing a single element. We can use Array.Copy() or Linq, but we need to modify them to remove multiple elements.

   public static int[] arrayremovemultipleelement(int[] array, int[] arrayindexremove)
        {
            int[] newarray = new int[array.Length - 1];
            int sourceindex = 0;
            int darrayindex = 0;

            for (int k=0; k<array.Length; k++)
            {
                if (!arrayindexremove.Contains(k))
                {
                    newarray[darrayindex] = array[sourceindex];
                    darrayindex++;

                }
                sourceindex++;
            }
            return newarray;
        }

Full Source Code

 namespace Remove_Array_Csharp 
{
    internal class Program
    {

        static void Main(string[] args)
        {

            int[] arrset = { 1, 4, 5, 6, 8, 4, 3 };

            int[] removeindex = { 5, 1 };
            int[] ints = arrayremovemultipleelement(arrset, removeindex);

            ints.ToList().ForEach(arr=> Console.WriteLine(arr.ToString())); 

        }

        public static int[] arrayCopy(int[] array, int index)
        {
            
            int arrayindex = Array.IndexOf(array, index);   

            int[] newarray= new int[array.Length - 1];
            
            Array.Copy(array,0,newarray,0, arrayindex);
            Array.Copy(array, arrayindex+1, newarray, arrayindex, array.Length - arrayindex - 1);
             
            return newarray;

        }

        public static int[] arrayremoveLinq(int[] array, int index)
        {

            int arrayindex = Array.IndexOf(array, index);
            int[] newarray = array.Where(arr => arr != index).ToArray(); 
            
           return newarray;
        }

        public static int[] arrayremoveAll(int[] array,int index)
        {
            var arrList = new List<int>(array);
            arrList.RemoveAll(arr => arr == index);

            return arrList.ToArray();
        }

        public static int[] arrayremoveat(int[] array, int index)
        {
            var arrList = new List<int>(array);
            arrList.RemoveAt(index);

            return arrList.ToArray();
        }

        public static int[] arrayremovemultipleelement(int[] array, int[] arrayindexremove)
        {             
            int[] newarray = new int[array.Length - 1];
            int sourceindex = 0;
            int darrayindex = 0;

            for (int k=0; k<array.Length; k++)
            {
                if (!arrayindexremove.Contains(k))
                {
                    newarray[darrayindex] = array[sourceindex];
                    darrayindex++;
                }
                sourceindex++;
            }
            return newarray;
        }
    }
}

More Related Post Remove All Whitespace From a String in C#

Conclusion
In this article, we have discussed several approaches for How to Remove/Delete Elements from an Array in C#. We have covered how to remove a single element. Depending on the specific use case and requirements, one approach may be more suitable than others.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments