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();
        }
    }
}

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

  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 to admin Cancel reply