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

Delete specified integer from an array in java is one of the common interview question.

java program to delete specified integer from an array

In the below java program first user enters elements and size of the array.

Then user enters the element to delete specified integer from an array in java.

Also read – abstraction in java

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

java program to delete specified integer from an array

Here’s the java program to delete specified integer from an array.

import java.util.Scanner;
public class DeleteSpecifiedInteger
{
   public static void main(String[] args)
   {
      int num, n, temp = 1, place = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println("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.println("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.println("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


Meanwhile we can also remove element from arraylist java.


Remove element by index

Now let’s learn to remove element from array by index.

So to remove an element by index from an array first we have to find element at specified index and remove that element.

Also read – remove duplicate element from array java

Then the remaining elements are copied to new array with size less than initial array.

Here’s the java program to remove element from a specific index from an array.

import java.util.Arrays;
public class DeleteElementDemo 
{
   // remove element method 
   public static int[] removeElement(int[] arrGiven, int index) 
   {
      // if empty 
      if(arrGiven == null || index < 0 || index >= arrGiven.length)
      {
         return arrGiven; 
      }
      // creating another array one 
      // less than initial array 
      int[] newArray = new int[arrGiven.length - 1]; 
      // copying elements except index 
      for(int a = 0, b = 0; a < arrGiven.length; a++) 
      {
         if(a == index) 
         { 
            continue; 
         } 
         newArray[b++] = arrGiven[a]; 
      } 
      return newArray; 
   }
   public static void main(String[] args) 
   { 
      int[] arrInput = { 2, 4, 6, 8, 10 };
      // printing given array 
      System.out.println("Given array: " + Arrays.toString(arrInput));
      // getting specified index 
      int index = 3;
      // print index 
      System.out.println("Index to be removed: " + index);
      // removing element 
      arrInput = removeElement(arrInput, index);
      // printing new array 
      System.out.println("New array: " + Arrays.toString(arrInput));
   }
}


Output:

Given array: [2, 4, 6, 8, 10]
Index to be removed: 3
New array: [2, 4, 6, 10]


program to insert and delete an element from an array in java

Here’s the program to insert an element from an array in java.

import java.util.Scanner;
public class InsertElement
{
   public static void main(String[] args)
   {
      int number, position, insert;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of elements in array: ");
      number = sc.nextInt();
      int[] arrNumbers = new int[number + 1];
      System.out.println("Please enter " + number + " elements: ");
      for(int a = 0; a < number; a++)
      {
         arrNumbers[a] = sc.nextInt();
      }
      System.out.println("Please enter position to insert element: ");
      position = sc.nextInt();
      System.out.println("Please enter element you want to insert: ");
      insert = sc.nextInt();
      for(int a = (number - 1); a >= (position - 1); a--)
      {
         arrNumbers[a + 1] = arrNumbers[a];
      }
      arrNumbers[position - 1] = insert;
      System.out.println("After inserting element: ");
      for(int a = 0; a < number; a++)
      {
         System.out.println(arrNumbers[a] + ",");
      }
      System.out.println(arrNumbers[number]);
      sc.close();
   }
}


Output:

Please enter number of elements in array: 6
Please enter 6 elements:
2
8
8
7
6
9
Please enter position to insert element: 3
Please enter element you want to insert: 1
After inserting element: 2,8,1,8,7,6,9


Here’s the program to delete an element from an array in java.

import java.util.Scanner;
public class DeleteElement
{
   public static void main(String[] args)
   {
      int number, delete, temp = 1, position = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of elements: ");
      number = sc.nextInt();
      int[] arrNumbers = new int[number];
      System.out.println("Please enter " + number + " elements: ");
      for(int a = 0; a < number; a++)
      {
         arrNumbers[a] = sc.nextInt();
      }
      System.out.println("Please enter element to delete: ");
      delete = sc.nextInt();
      for(int a = 0; a < number; a++)
      {
         if(arrNumbers[a] == delete)
         {
            temp = 1;
            position = a;
            break;
         }
         else
         {
            temp = 0;
         }
      }
      if(temp == 1)
      {
         for(int a = position + 1; a < number; a++)
         {
            arrNumbers[a - 1] = arrNumbers[a];
         }
         System.out.println("After Deleting: ");
         for(int a = 0; a < number - 2; a++)
         {
            System.out.println(arrNumbers[a] + ",");
         }
         System.out.println(arrNumbers[number - 2]);
      }
      else
      {
         System.out.println("Element not found.");
      }
      sc.close();
   }
}


Output:

Please enter number of elements: 6
Please enter 6 elements:
2
4
6
8
1
3
Please enter element to delete: 8
After Deleting:
2,
4,
6,
1,
3

Please enter number of elements: 6
Please enter 6 elements:
1
3
5
7
9
2
Please enter element to delete:
8
Element not found.


remove element from string array java

To remove element from string array; first create a string array. Then save this string array to list.

Now using remove() method in List class remove element from string array.

Here’s the program on remove element from string array java.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class RemoveElementString
{
   public static void main(String[] args)
   {
      String[] strFruits = {"apple","orange","pineapple","banana"};
      System.out.println("Before remove element from string array: " + Arrays.toString(strFruits));
      List<String> liFruits = new ArrayList<String>(Arrays.asList(strFruits));
      liFruits.remove("pineapple");
      strFruits = liFruits.toArray(new String[0]);
      System.out.println("Before remove element from string array: " + Arrays.toString(strFruits));
   }
}


Output:

Before – remove element from string array: [apple, orange, pineapple, banana]
After – remove element from string array: [apple, orange, banana]


how to remove multiple elements from array in java

To remove multiple elements from array in java use ArrayUtils.removeAll(String[] array, int… indices) method. Here’s the java program.

import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
public class RemoveMultipleElements
{
   public static void main(String[] args)
   {
      String[] strFruits = {"apple","orange","pineapple", "banana","apricot","grape","mango"};
      System.out.println("Before - remove multiple elements from array");
      System.out.println(Arrays.toString(strFruits));
      String[] fruits = ArrayUtils.removeAll(strFruits, 1, 3, 5);
      System.out.println("After - remove multiple elements from array");
      System.out.println(Arrays.toString(fruits));
   }
}


Output:

Before – remove multiple elements from array
[apple, orange, pineapple, banana, apricot, grape, mango]
After – remove multiple elements from array
[apple, pineapple, apricot, mango]