Java Programt To Delete the Specified Integer From an Array
In this tutorial, we will learn how to delete a specific element from an array. The easiest way to remove an element in an array is by shifting the elements by one index to the left from where we want to remove the element. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Input: 5 9 8 3 2 6 7
Output: Element to be deleted: 8
Array: 5 9 3 2 6 7
Program 1: How to Delete a Specific Element from an Array
In this approach, we will traverse through all the elements and shift the elements to the left by 1 index wherever the element to be deleted is found.
Algorithm
- Start
- Declare an array
- Initialize the array.
- Declare the element to be deleted.
- Using a for loop iterate through all the elements of the array.
- If the element is found, then start shifting the elements after that index to the left by one element.
- Now, print the updated array.
- Stop
Below is the code for the same.
The below program demonstrates how to delete a specific element from an array by traversing through all the elements.
/*Java Program to delete an element from an Array*/
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n; //Array Size Declaration
System.out.println("Enter the number of elements :");
n=sc.nextInt(); //Array Size Initialization
Integer arr[]=new Integer[n]; //Array Declaration
System.out.println("Enter the elements of the array :");
for(int i=0;i<n;i++) //Array Initialization
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the element you want to remove ");
int elem = sc.nextInt();
for(int i = 0; i < arr.length; i++)
{
if(arr[i] == elem) //If element found
{
// shifting elements
for(int j = i; j < arr.length - 1; j++)
{
arr[j] = arr[j+1];
}
break;
}
}
System.out.println("Elements after deletion " );
for(int i = 0; i < arr.length-1; i++)
{
System.out.print(arr[i]+ " ");
}
}
}
Enter the number of elements :10
Enter the elements of the array :
1 2 3 4 5 6 7 8 9 10
Enter the element you want to remove
5
Elements after deletion
1 2 3 4 6 7 8 9 10
Program 2: How to Delete a specific element from an Array
In this approach, we will use Collection API to remove an element from an array. Firstly, we will convert an array to an array list and then remove the specific element. After the removal of the element, we will convert the array list back to an array.
Algorithm
- Start
- Declare an array
- Initialize the array.
- Declare the element to be deleted.
- Using a for loop iterate through all the elements of the array.
- If the element is found, then call a separate method to delete the element.
- Convert the array into an array list.
- Now, remove the element.
- Convert it back to an array.
- Now, print the updated array.
- Stop.
Below is the code for the same.
The below program demonstrates how to delete a specific element from an array by using Collection API provided by the Java language.
/*Java Program to delete an element in an Array*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class RemoveElement
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n; // Array Size Declaration
System.out.println("Enter the number of elements :");
n=in.nextInt(); // Array Size Initialization
Integer arr[]=new Integer[n]; // Array Declaration
System.out.println("Enter the elements of the array :");
for(int i=0;i<n;i++) // Array Initialization
{
arr[i]=in.nextInt();
}
System.out.print("Enter Element to be deleted : ");
int elem = in.nextInt(); //Initializing Element
System.out.println("Original Array " + Arrays.toString(arr));
for(int i = 0; i < arr.length; i++)
{
if(arr[i] == elem)
{
arr = removeElementUsingCollection(arr, i);
break;
}
}
System.out.println("Array after removal of Element -- " );
for(int i = 0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
static Integer[] removeElementUsingCollection( Integer[] arr, int index )
{
List<Integer> tempList = new ArrayList<Integer>(Arrays.asList(arr));
tempList.remove(index);
return tempList.toArray(new Integer[0]);
}
}
Enter the number of elements:10
Enter the elements of the array :
1 2 3 4 5 6 7 8 9 10
Enter Element to be deleted : 8
Original Array
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Array after removal of Element --
1 2 3 4 5 6 7 9 10