Remove an Element at specific index from an Array in Java
Given an array of fixed length. The task is to remove an element at a specific index from the array.
Examples:
Input: arr[] = { 1, 2, 3, 4, 5 }, index = 2
Output: arr[] = { 1, 2, 4, 5 }
Input: arr[] = { 4, 5, 9, 8, 1 }, index = 3
Output: arr[] = { 4, 5, 9, 1 }
- Naive or Basic approach (Using another array): The basic approach includes finding the element at the specified index and then removing that element. The rest of the elements are copied into a new array. This would lead to an array of size one less than the original array.
Below is the implementation of the above approach:
// Java program to remove an element// from a specific index from an arrayimportjava.util.Arrays;classGFG {// Function to remove the elementpublicstaticint[] removeTheElement(int[] arr,intindex){// If the array is empty// or the index is not in array range// return the original arrayif(arr ==null|| index <0|| index >= arr.length) {returnarr;}// Create another array of size one lessint[] anotherArray =newint[arr.length -1];// Copy the elements except the index// from original array to the other arrayfor(inti =0, k =0; i < arr.length; i++) {// if the index is// the removal element indexif(i == index) {continue;}// if the index is not// the removal element indexanotherArray[k++] = arr[i];}// return the resultant arrayreturnanotherArray;}// Driver Codepublicstaticvoidmain(String[] args){// Get the arrayint[] arr = {1,2,3,4,5};// Print the resultant arraySystem.out.println("Original Array: "+ Arrays.toString(arr));// Get the specific indexintindex =2;// Print the indexSystem.out.println("Index to be removed: "+ index);// Remove the elementarr = removeTheElement(arr, index);// Print the resultant arraySystem.out.println("Resultant Array: "+ Arrays.toString(arr));}}chevron_rightfilter_noneOutput:Original Array: [1, 2, 3, 4, 5] Index to be removed: 2 Resultant Array: [1, 2, 4, 5]
- Using Java 8 Streams:
Approach:
- Get the array and the index.
- Convert the array into IntStream using IntStream.range() method.
- Remove the specified index element using filter() method.
- Map and form a new array of the filtered elements using map() and toArray() methods.
- Return the formed array.
Below is the implementation of the above approach:
// Java program to remove an element// from a specific index from an arrayimportjava.util.Arrays;importjava.util.stream.IntStream;classGFG {// Function to remove the elementpublicstaticint[] removeTheElement(int[] arr,intindex){// If the array is empty// or the index is not in array range// return the original arrayif(arr ==null|| index <0|| index >= arr.length) {returnarr;}// return the resultant arrayreturnIntStream.range(0, arr.length).filter(i -> i != index).map(i -> arr[i]).toArray();}// Driver Codepublicstaticvoidmain(String[] args){// Get the arrayint[] arr = {1,2,3,4,5};// Print the resultant arraySystem.out.println("Original Array: "+ Arrays.toString(arr));// Get the specific indexintindex =2;// Print the indexSystem.out.println("Index to be removed: "+ index);// Remove the elementarr = removeTheElement(arr, index);// Print the resultant arraySystem.out.println("Resultant Array: "+ Arrays.toString(arr));}}chevron_rightfilter_noneOutput:Original Array: [1, 2, 3, 4, 5] Index to be removed: 2 Resultant Array: [1, 2, 4, 5]
- Using ArrayList:
Approach:
- Get the array and the index.
- Form an ArrayList with the array elements.
- Remove the specified index element using remove() method.
- Form a new array of the ArrayList using mapToInt() and toArray() methods.
- Return the formed array.
Below is the implementation of the above approach:
// Java program to remove an element// from a specific index from an arrayimportjava.util.Arrays;importjava.util.List;importjava.util.stream.Collectors;importjava.util.stream.IntStream;classGFG {// Function to remove the elementpublicstaticint[] removeTheElement(int[] arr,intindex){// If the array is empty// or the index is not in array range// return the original arrayif(arr ==null|| index <0|| index >= arr.length) {returnarr;}// Create ArrayList from the arrayList<Integer> arrayList = IntStream.of(arr).boxed().collect(Collectors.toList());// Remove the specified elementarrayList.remove(index);// return the resultant arrayreturnarrayList.stream().mapToInt(Integer::intValue).toArray();}// Driver Codepublicstaticvoidmain(String[] args){// Get the arrayint[] arr = {1,2,3,4,5};// Print the resultant arraySystem.out.println("Original Array: "+ Arrays.toString(arr));// Get the specific indexintindex =2;// Print the indexSystem.out.println("Index to be removed: "+ index);// Remove the elementarr = removeTheElement(arr, index);// Print the resultant arraySystem.out.println("Resultant Array: "+ Arrays.toString(arr));}}chevron_rightfilter_noneOutput:Original Array: [1, 2, 3, 4, 5] Index to be removed: 2 Resultant Array: [1, 2, 4, 5]
- Using System.arraycopy():
Approach:
- Get the array and the index.
- Create a new array of size one less than the size of original array.
- Copy the elements from starting till index from original array to the other array using System.arraycopy()
- Copy the elements from index + 1 till end from original array to the other array using System.arraycopy()
- Return the formed array.
Below is the implementation of the above approach:
// Java program to remove an element// from a specific index from an arrayimportjava.util.Arrays;classGFG {// Function to remove the elementpublicstaticint[] removeTheElement(int[] arr,intindex){// If the array is empty// or the index is not in array range// return the original arrayif(arr ==null|| index <0|| index >= arr.length) {returnarr;}// Create another array of size one lessint[] anotherArray =newint[arr.length -1];// Copy the elements from starting till index// from original array to the other arraySystem.arraycopy(arr,0, anotherArray,0, index);// Copy the elements from index + 1 till end// from original array to the other arraySystem.arraycopy(arr, index +1,anotherArray, index,arr.length - index -1);// return the resultant arrayreturnanotherArray;}// Driver Codepublicstaticvoidmain(String[] args){// Get the arrayint[] arr = {1,2,3,4,5};// Print the resultant arraySystem.out.println("Original Array: "+ Arrays.toString(arr));// Get the specific indexintindex =2;// Print the indexSystem.out.println("Index to be removed: "+ index);// Remove the elementarr = removeTheElement(arr, index);// Print the resultant arraySystem.out.println("Resultant Array: "+ Arrays.toString(arr));}}chevron_rightfilter_noneOutput:Original Array: [1, 2, 3, 4, 5] Index to be removed: 2 Resultant Array: [1, 2, 4, 5]
Recommended Posts:
- Find the index of an array element in Java
- Remove all occurrences of an element from Array in Java
- Replace a character at a specific index in a String in Java
- List remove(int index) method in Java with Examples
- List add(int index, E element) method in Java
- How to remove an element from ArrayList in Java?
- Creating a Cell at specific position in Excel file using Java
- ArrayList get(index) method in Java with examples
- WeakHashMap remove() method in Java
- IdentityHashMap remove() Method in Java
- HashSet remove() Method in Java
- Queue remove() method in Java
- Map remove() Method in Java with Examples
- ConcurrentLinkedQueue remove() method in Java
- How to Remove Duplicates from ArrayList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



