February 8, 2022

Remove Element From an Array in Java

In this post we’ll see how to remove or delete element from an array in Java. You have several options to do that task of removing an element from an array.

  1. Write your own logic. See example.
  2. Use System.arraycopy() method for removing element from an array. See example.
  3. Use Apache Commons library. In that library there is a ArrayUtils class that has remove method for that purpose. See example.
  4. Use ArrayList to remove an element. You will need to convert array to ArrayList and then back to array. See example.

Writing your own logic

If you have to write your own Java program to remove element from an array then you will have to shift all the elements, to the left, that come after the element that has to be removed. Another thing that you will have to consider is; once created array is of fixed length so after shifting all the elements there will still be one space left in the array and that space will be filled by repeating the last element. Let’s see with an example.

Java Program to remove element from an array

import java.util.Scanner;

public class RemoveArrayElement {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] numArray = {6, 8, 10, 34, 12, 2};

    System.out.print("Enter Element to be deleted: ");
    int element = in.nextInt();
    
    for(int i = 0; i < numArray.length; i++){
      if(numArray[i] == element){
        // shift elements to the left
        for(int j = i; j < numArray.length - 1; j++){
          numArray[j] = numArray[j+1];
        }
        break;
      }
    }
    
    System.out.println("Array elements after deletion-- " );
    for(int i = 0; i < numArray.length; i++){
      System.out.print(" " + numArray[i]);
    }  
  }
}
Output
Enter Element to be deleted: 10
Array elements after deletion-- 
 6 8 34 12 2 2

You can see the empty space after shifting the elements is filled by repeating the last element.

What you can do is to create another array of size one less than the original array. After shifting the elements you can copy element from the original array to the new array, leaving the last element. For copying the array you can use System.arraycopy() method.

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)- Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. The number of components copied is equal to the length argument.

import java.util.Scanner;

public class RemoveArrayElement {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] numArray = {6, 8, 10, 34, 12, 2};
    
    System.out.print("Enter Element to be deleted: ");
    int element = in.nextInt();
    int[] newArray = new int[numArray.length - 1];
    
    for(int i = 0; i < numArray.length; i++){
      if(numArray[i] == element){
        // shift elements to the left
        for(int j = i; j < numArray.length - 1; j++){
          numArray[j] = numArray[j+1];
        } 
        // Copy array
        System.arraycopy(numArray, 0, newArray, 0, numArray.length - 1);
        break;
      }
    }
    
    System.out.println("Array elements after deletion-- " );
    for(int i = 0; i < newArray.length; i++){
      System.out.print(" " + newArray[i]);
    }  
  }
}
Output
Enter Element to be deleted: 10
Array elements after deletion-- 
 6 8 34 12 2

As you can see now the problem of extra element is resolved.

Using System.arraycopy() method to delete array element

You can use System.arraycopy() method to remove element from an array in Java. Once you know the index of the element that has to be removed you can call System.arraycopy() method twice, once for copying the element from 0 till the index and then from index + 1 till the end of the array.

import java.util.Scanner;

public class RemoveArrayElement {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] numArray = {6, 8, 10, 34, 12, 2};

    System.out.print("Enter Element to be deleted: ");
    int element = in.nextInt();
    int[] newArray = new int[numArray.length - 1];
    
    for(int i = 0; i < numArray.length; i++){
      if(numArray[i] == element){
        // Copy array
        System.arraycopy(numArray, 0, newArray, 0, i);
        System.arraycopy(numArray, i + 1, newArray, i, (numArray.length - (i+1)));
        break;
      }
    }
    
    System.out.println("Array elements after deletion-- " );
    for(int i = 0; i < newArray.length; i++){
      System.out.print(" " + newArray[i]);
    }  
  }
}
Output
Enter Element to be deleted: 34
Array elements after deletion-- 
 6 8 10 12 2

Enter Element to be deleted: 6
Array elements after deletion-- 
 8 10 34 12 2

Using ArrayUtils class in Apache Commons

You can also use ArrayUtils.remove() method in ArrayUtils class to remove element from an array in Java. For that you will have to have Apache commons jar in classpath.

import java.util.Scanner;

import org.apache.commons.lang3.ArrayUtils;

public class RemoveArrayElement {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] numArray = {6, 8, 10, 34, 12, 2};

    System.out.print("Enter Element to be deleted: ");
    int element = in.nextInt();
    
    for(int i = 0; i < numArray.length; i++){
      if(numArray[i] == element){
        // Using ArrayUtils class
        numArray = ArrayUtils.remove(numArray, i);
        break;
      }
    }
    
    System.out.println("Array elements after deletion-- " );
    for(int i = 0; i < numArray.length; i++){
      System.out.print(" " + numArray[i]);
    }  
  }
}
Output
Enter Element to be deleted: 12
Array elements after deletion-- 
 6 8 10 34 2

Converting array to ArrayList to remove element from array

You can convert array to ArrayList and then remove the element from that ArrayList. After removal of element you can convert ArrayList back to array.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class RemoveArrayElement {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Integer[] numArray = {6, 8, 10, 34, 12, 2};
    System.out.print("Enter Element to be deleted: ");
    int element = in.nextInt();
    
    for(int i = 0; i < numArray.length; i++){
      if(numArray[i] == element){
        numArray = removeElement(numArray, i);
        break;
      }
    }
    
    System.out.println("Array elements after deletion-- " );
    for(int i = 0; i < numArray.length; i++){
      System.out.print(" " + numArray[i]);
    }  
  }

  public static Integer[] removeElement( Integer[] numArray, int index ){
    // Converting to list
    List tempList = new ArrayList(Arrays.asList(numArray));
    tempList.remove(index);
    // converting back to array
    return tempList.toArray(new Integer[0]);
  }
}
Output
Enter Element to be deleted: 8
Array elements after deletion-- 
 6 10 34 12 2

That's all for the topic Remove Element From an Array in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment