Java program to delete specified integer from an array

Let’s learn java program to delete specified integer from an array.

java program to delete specified integer from an array

Java program to delete the specified integer from an array

To remove element from array java user enters elements and size of the array. Then user enters the element the user want to delete.

Also read – abstraction in java

For this we have to first find location of element and then shift positions of all the elements.

java program to delete specified integer from an array

Here’s the java program on how to remove an element at specific index from an array in java,

import java.util.Scanner;

public class DeleteElementFromArray
{
   public static void main(String[] args)
   { 
      int num, n, temp = 1, place = 0;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter number of elements : ");
      num = sc.nextInt();
      int arrNum[] = new int[num];
      System.out.println("Please enter all the elements : ");
      for(int a = 0; a < num; a++) 
      {
         arrNum[a] = sc.nextInt();
      }
      System.out.print("Enter the element you want to delete : ");
      n = sc.nextInt();
      for(int a = 0; a < num; a++) 
      {
         if(arrNum[a] == n)
         {
            temp = 1;
            place = a;
            break;
         }
         else
         {
            temp = 0;
         }
      }
      if(temp == 1)
      {
         for(int a = place + 1; a < num; a++)
         {
            arrNum[a - 1] = arrNum[a];
         }
         System.out.print("After deleting element : ");
         for(int a = 0; a < num - 2; a++) 
         {
            System.out.print(arrNum[a] + ",");
         }
         System.out.print(arrNum[num - 2]);
      }
      else
      {
         System.out.println("Element not found!!");
      }
      sc.close();
   }
}



Output:

Please enter number of elements : 5
Please enter all the elements :
2
8
4
9
7
Enter the element you want to delete : 9
After deleting element : 2,8,4,7


Similarly to remove an element at specific index from an array in java we can use java 8 streams.

Here, first we have to get the array and its index. Then array is converted to IntStream using range() method.

In the next step using filter() method specified index is removed. Now new array of filtered elements is formed using map() and toArray() method. Lastly return and print new array.

import java.util.Arrays;
import java.util.stream.IntStream;

public class RemoveElementDemo 
{
   public static int[] remove(int arrInput[], int index) 
   {
      // checking if the array is empty 
      // or the index not in range
      if(arrInput == null || index < 0 || index >= arrInput.length) 
      {
         return arrInput; 
      } 

      // return the resultant array 
      return IntStream.range(0, arrInput.length).
filter(i -> i != index).
map(i -> arrInput[i]).toArray();
   }

   public static void main(String[] args) 
   { 
      int arrNum[] = { 65, 36, 29, 59, 86 };
      // printing given array 
      System.out.println("Given array : " + Arrays.toString(arrNum));
      // specific index 
      int index = 3;
      // printing specified index 
      System.out.println("Index to be removed : " + index);
      // removing element 
      arrNum = remove(arrNum, index);
      // print new array 
      System.out.println("New array: " + Arrays.toString(arrNum));
   }
}



Output:

Given array : [65, 36, 29, 59, 86]
Index to be removed : 3
New array: [65, 36, 29, 86]


Meanwhile above java program can be executed using arraycopy method and arraylist.