Delete an Element from an Array in Java

Deleting from a given Index

Write a program to allow the user to enter N integers into a one-dimensional array. The value of N is to be input by the user.

Then ask the user to enter the index from where he/she wants to delete an element. Perform the deletion and then display the resultant array.

import java.io.*;
class MyArray{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Array size: ");
        int size = Integer.parseInt(br.readLine());
        int a[] = new int[size];
        System.out.println("Enter array elements:");
        for(int i = 0; i < size; i++)
            a[i] = Integer.parseInt(br.readLine());
        System.out.print("Index to delete from: ");
        int index = Integer.parseInt(br.readLine());
        if(index < 0 || index >= size){
            System.out.println("Invalid index!");
            return;
        }
        if(index == size - 1)
            a[index] = 0;
        else{
            for(int i = index + 1; i < size; i++)
                a[i - 1] = a[i];
            a[size - 1] = 0;
        }
        System.out.println("Resultant array after deletion:");
        for(int i = 0; i < size; i++)
            System.out.print(a[i] + "\t");
        System.out.println();
    }
}

Deleting by searching for the element

Another question could be to allow the user to enter the element value that he/she wants to delete from an array.

If that element exists in the array, then the element is deleted successfully, otherwise the user is informed that the element does not exist.

Below is the program that solves this problem:

import java.io.*;
class DeleteFromArray{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Array size: ");
        int size = Integer.parseInt(br.readLine());
        int a[] = new int[size];
        System.out.println("Enter array elements:");
        for(int i = 0; i < size; i++)
            a[i] = Integer.parseInt(br.readLine());
        System.out.println("Enter element to be deleted: ");
        int element = Integer.parseInt(br.readLine());
        int i = 0;
        for(i = 0; i < size; i++){
            if(element == a[i]){
                int temp[] = new int[size - 1];
                int index = 0;
                for(int j = 0; j < i; j++){
                    temp[index++] = a[j];
                }
                for(int j = i + 1; j < size; j++){
                    temp[index++] = a[j];
                }
                a = temp;
                break;
            }
        }
        if(i == size)
            System.out.println(element + " not found.");
        else{
            System.out.println("New array:");
            for(i = 0; i < a.length; i++)
                System.out.print(a[i] + "\t");
            System.out.println();
        }
    }
}

13 thoughts on “Delete an Element from an Array in Java

    • Sir,in the above program while deleting an element through index no. the output is showing like this:
      Array size: 10
      Enter array elements:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      Index to delete from: 0
      Resultant array after deletion:
      2 3 4 5 6 7 8 9 10 0
      But Sir,Deletion means to remove an element from an array but the last element is showing 0.Why is it so?

      • After deletion, the remaining elements shift one by one to fill up the gap created. So the second element becomes the first, the third becomes second, and so on.

        • But Sir,deletion of the array order should be
          2 3 4 5 6 7 8 9 10
          The size should be one less Sir than the original array for above program (deletion from specific index)

          • Since the array size remains fixed, we cannot modify the size of the original array.

    • Sir,a program is given which states that:
      Write a program to allow the user to enter integers into a two-dimensional array of size M × M

      The size of the rows and columns is also entered by the user.(M must be greater than 2 and less than 10)

      Display the original matrix.
      Now sort the integers diagonal-wise in ascending order in the matrix, and display the resultant matrix.

        • Yes Sir .
          Can you show me the code for sorting each diagonal separately for this program:

          Write a program to allow the user to enter integers into a two-dimensional array of size M × M

          The size of the rows and columns is also entered by the user.(M must be greater than 2 and less than 10)

          Display the original matrix.
          Now sort each diagonal in ascending order in the matrix, and display the resultant matrix.

  1. Sir,a program is given which states that
    Write a program to allow the user to enter integers into a two-dimensional array of size M × N.

    The size of the rows and columns is also entered by the user.(M & N must be greater than 2 and less than 10)

    Display the original matrix.

    Now sort the integers row-wise in ascending order in the matrix, and display the resultant matrix.
    How to solve the program by creating a single dimensional array and passing each elements in the row in a single dimensional array?
    INPUT:
    m = 4
    n = 3
    Enter elements of matrix:

    11 -2 3
    5 16 7
    9 0 4
    3 1 8
    OUTPUT:
    Original Matrix:

    11 -2 3
    5 16 7
    9 0 4
    3 1 8
    Matrix after sorting rows:

    -2 3 11
    5 7 16
    0 4 9
    1 3 8

  2. Sir,in the deletion of an element in array from a specified index in java ,the loop will run from 0 to size-1 or from 0 to size -2?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.