Java Program to delete element from array at given index

Java Program to delete element from array at given index

There are multiple approaches to delete an element from an array at a given location.

We can delete the element from a given index of an array by using a second array, using the java 8 streams () method, or using ArrayList.

For example

Suppose we have an array arr[] = [2, 4, 6, 8, 10]

After deleting the element from index 2 output will be [2, 4, 8, 10]

Here at index 2 there was element 6.

Now lets see the program

Program 1: Delete element from the given index using the second array

  • In the below program, we are taking the array elements and array size from the user.
  • To delete the element from the given index, we will be using the second array.
  • We will be copying the array element from the original array to the new array.
  • We are skipping the index from where we have to remove the element and copy the rest array element.
  • By doing this in the new array, we are getting all elements except the element which is present at the index that need to delete.
import java.util.*;
public class Main {
    public static void main(String[] args) {
        int index;
		Scanner sc = new Scanner(System.in);
		System.out.println("Java Program to delete element from given index");
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr[i] = sc.nextInt();
        }
        System.out.println("Enter the index to delete element :");
        index=sc.nextInt();
        if (arr == null || index < 0
            || index >= arr.length) {
            System.out.println(Arrays.toString(arr));
        }
        int[] arrOutput = new int[arr.length - 1];
        for (int i = 0, j = 0; i < arr.length; i++) {
            if (i == index) {
                continue;
            }
            arrOutput[j++] = arr[i];
        }
        System.out.println("Resultant Array: "
                           + Arrays.toString(arrOutput));
    }
}

Output

Java Program to delete element from given index
Enter the size of array: 5
Please give value for index 0 : 2
Please give value for index 1 : 4
Please give value for index 2 : 1
Please give value for index 3 : 5
Please give value for index 4 : 6
Enter the index to delete element :
2
Resultant Array: [2, 4, 5, 6]

Program 2 : Delete an element from a given index using Arraylist

  • Here we are taking the help of remove() function of arraylist.
  • First we are taking the array size and array input.
  • We are converting array into arraylist to use the remove() function of array list.
import java.util.stream.Collectors;
import java.util.*;
class Main {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
		System.out.println("Java Program to delete element from given index");
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();//taking array size from users
        int arr[] = new int[size]; //creating array of given size
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr[i] = sc.nextInt(); //taking values at each index of array
        }
        System.out.println("Original Array: " + Arrays.toString(arr));
        System.out.println("Enter the index to delete element :");
        int index=sc.nextInt(); //taking index from users to perform delete
        //converting array into arraylist
        List<Integer> arrayList  = Arrays.stream( arr ).boxed().collect( Collectors.toList() );
        arrayList.remove(index); //removing value from given index
        System.out.println("Array after performing delete operation: "
                        + Arrays.toString(arrayList.toArray())); //printing the new Resultant array
    }
}

Output

Java Program to delete element from given index
Enter the size of array: 5
Please give value for index 0 : 3
Please give value for index 1 : 1
Please give value for index 2 : 4
Please give value for index 3 : 2
Please give value for index 4 : 5
Enter the index to delete element :
3
Array After removing element from given location: 
[3, 1, 4, 5]

Program 3: Using JAVA 8 Stream API

  • Here we are getting array size and array inputs from the users.
  • Using IntStream.range() we are converting input array into IntStream.
  • With the help of filter() method we are removing array elements.
  • Once the element has remove from the array using filter() method, printing the result using Arrays.toString(result).
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.*;
class Main {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
		System.out.println("Java Program to delete element from given index");
        System.out.print("Enter the size of array: ");
        //taking array size from users
        int size = sc.nextInt();
        //creating array of given size
        int arr[] = new int[size]; 
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            //taking values at each index of array
            arr[i] = sc.nextInt(); 
        }
        System.out.println("Original Array: " + Arrays.toString(arr));
        System.out.println("Enter the index to delete element :");
        //taking index from users to perform delete
        int index=sc.nextInt(); 
        int result[]=IntStream.range(0, arr.length)
            .filter(i -> i != index)
            .map(i -> arr[i])
            .toArray();
        //printing the new Resultant array
        System.out.println("Array after performing delete operation: "
                        + Arrays.toString(result)); 
    }
}

Output

Please give value for index 0 : 3
Please give value for index 1 : 1
Please give value for index 2 : 5
Please give value for index 3 : 2
Please give value for index 4 : 7
Original Array: [3, 1, 5, 2, 7]
Enter the index to delete element :
3
Array after performing delete operation: [3, 1, 5, 7]