How to remove elements from an array in Java?

An element can be removed from the array using its index value or by using the element value itself.

Using the index value and for loop

An array element can be removed using for loop and the index position of the element. In this method, we need to create a new array.

First, the index of the array element which has to be deleted is obtained. Then all the elements are copied to the new array except the one to be removed.

Let us consider an array old[] = {10, 12, 14, 16, 18, 20}. Suppose we have to remove an element at index position 2(which is 14).

The example below illustrates how to remove the element using for loop.

import java.util.*;
public class Main {
  public static void main(String[] args) {
    int[] old = new int[] {10, 12, 14, 16, 18, 20};
    int[] new_arry = new int[old.length - 1];
    int j = 2;
    for (int i = 0, k = 0; i < old.length; i++) {
      if (i != j) {
        new_arry[k] = old[i]; // copying elements to new array
        k++;
      }
    }
    System.out.println("Old array :" + Arrays.toString(old));
    System.out.println("Index position to be deleted = " + j);
    System.out.println("New array :" + Arrays.toString(new_arry));
  }
}

The output is

Old array :[10, 12, 14, 16, 18, 20]
Index position to be deleted = 2
New array :[10, 12, 16, 18, 20]

Using the value of the element and for loop

An array element can be removed using its value and for loop. A new empty array is created for this method.

First, the value of the element to be removed is copied to a variable. Then the array is iterated and all the elements expect the element to be removed are copied to the new array.

Let us consider an array a[] = {45, 55, 56, 65, 75, 85}. Program below illustrates the method to remove the element 56.

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    int[] a = new int[] {35, 45, 55, 56, 65};
    int[] b = new int[a.length - 1];
    int j = 56;
    for (int i = 0, k = 0; i < a.length; i++) {
      if (a[i] != j) {
        b[k] = a[i];
        k++;
      }
    }
    System.out.println("Before deletion :" + Arrays.toString(a));
    System.out.println("After deletion :" + Arrays.toString(b));
  }
}

The output is

Before deletion :[35, 45, 55, 56, 65]
After deletion :[35, 45, 55, 65]

Using System.arraycopy()

An array element can be removed using System.arraycopy() method. In this method a new empty array is created.

First, we copy the elements from the original array into the new array starting from 0 to index position. Then we copy the elements from the original array into the new array from index+1 up to the length of the array.

In this method, we are skipping the element to be removed and copying the rest of the array.

Let us consider an array arry [] = {13, 26, 39, 42, 52}. The element to be removed is 42. Example below illustrates how to remove an element from array using System.arraycopy().

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    int[] arry = {13, 26, 39, 42, 52};
    System.out.println("Original Array: " +
      Arrays.toString(arry));
    int index = 3;
    System.out.println("Element to be deleted at index: " + index);
    int[] new_arry = new int[arry.length - 1];
    System.arraycopy(arry, 0, new_arry, 0, index);
    System.arraycopy(arry, index + 1, new_arry, index, arry.length - index - 1);
    System.out.println("New array: " +
      Arrays.toString(new_arry));
  }
}

The output is

Original Array: [13, 26, 39, 42, 52]
Element to be deleted at index: 3
New array: [13, 26, 39, 52]

Using Java 8 streams

The Java8 streams can be used to an remove an element from array.

In this method, first the array is converted to a stream. Then the element at the specified index is deleted using filter the method.

After the element is removed, the "map" and "toArray" methods is used and the stream is converted back to array.

Let us consider an array old [] = {7, 14, 21, 28, 35, 40, 42}. The program below illustrates how to remove the element at index position 5(which is 40).

import java.util.Arrays;
import java.util.stream.IntStream;
public class Example {
  public static int[] removeElement(int[] arr, int index) {
  // return if it is invalid array
    if (arr == null || index < 0 || index >= arr.length) {
      return arr;
    }
    return IntStream.range(0, arr.length).filter(i -> i != index)
    .map(i -> arr[i]).toArray();
  }
  public static void main(String[] args) {
    int[] old = {7, 14, 21, 28, 35, 40, 42};
    System.out.println("Original Array: " + Arrays.toString(old));
    int index = 5;
    System.out.println("Element to be removed at index: " + index);
    old = removeElement(old, index);
    System.out.println("Resultant Array: " + Arrays.toString(old));
  }
}

The output is

Original Array: [7, 14, 21, 28, 35, 40, 42]
Element to be removed at index: 5
Resultant Array: [7, 14, 21, 28, 35, 42]

Removing array element using ArrayList

The remove() method of ArrayList can be used to remove an element from the array.

In this method, first the array is converted to ArrayList. Then the element at the specified index is removed using remove() method.

Let us consider an array original[] = {10, 20, 30, 35, 40, 50}. The program below illustrates how to remove the element at index 3(which is 35).

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class arrayList {
  public static int[] removeTheElement(int[] ar,
    int index) {
    if (ar == null || index < 0 || index >= ar.length) {
      return ar;
    }
    // Creating a ArrayList from the array
    List < Integer > al = IntStream.of(ar)
      .boxed()
      .collect(Collectors.toList());
    al.remove(index); // removing the specified element
    return al.stream().mapToInt(Integer::intValue).toArray();
  }
  public static void main(String[] args) {
    int[] original = {10, 20, 30, 35, 40, 50};
    System.out.println("Original Array: " +
      Arrays.toString(original));
    int index = 3;
    System.out.println("Element to be removed at index: " + index);
    original = removeTheElement(original, index);
    System.out.println("Modified Array: " + Arrays.toString(original));
  }
}

The output is

Original Array: [10, 20, 30, 35, 40, 50]
Element to be removed at index: 3
Modified Array: [10, 20, 30, 40, 50]
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions