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 }
  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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to remove an element
    // from a specific index from an array
      
    import java.util.Arrays;
      
    class GFG {
      
        // Function to remove the element
        public static int[] removeTheElement(int[] arr,
                                              int index)
        {
      
            // If the array is empty
            // or the index is not in array range
            // return the original array
            if (arr == null
                || index < 0
                || index >= arr.length) {
      
                return arr;
            }
      
            // Create another array of size one less
            int[] anotherArray = new int[arr.length - 1];
      
            // Copy the elements except the index
            // from original array to the other array
            for (int i = 0, k = 0; i < arr.length; i++) {
      
                // if the index is
                // the removal element index
                if (i == index) {
                    continue;
                }
      
                // if the index is not
                // the removal element index
                anotherArray[k++] = arr[i];
            }
      
            // return the resultant array
            return anotherArray;
        }
      
        // Driver Code
        public static void main(String[] args)
        {
      
            // Get the array
            int[] arr = { 1, 2, 3, 4, 5 };
      
            // Print the resultant array
            System.out.println("Original Array: "
                               + Arrays.toString(arr));
      
            // Get the specific index
            int index = 2;
      
            // Print the index
            System.out.println("Index to be removed: "
                               + index);
      
            // Remove the element
            arr = removeTheElement(arr, index);
      
            // Print the resultant array
            System.out.println("Resultant Array: "
                               + Arrays.toString(arr));
        }
    }

    chevron_right

    
    

    Output:

    Original Array: [1, 2, 3, 4, 5]
    Index to be removed: 2
    Resultant Array: [1, 2, 4, 5]
    
  2. Using Java 8 Streams:

    Approach:



    1. Get the array and the index.
    2. Convert the array into IntStream using IntStream.range() method.
    3. Remove the specified index element using filter() method.
    4. Map and form a new array of the filtered elements using map() and toArray() methods.
    5. Return the formed array.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to remove an element
    // from a specific index from an array
      
    import java.util.Arrays;
    import java.util.stream.IntStream;
      
    class GFG {
      
        // Function to remove the element
        public static int[] removeTheElement(int[] arr, 
                                              int index)
        {
      
            // If the array is empty
            // or the index is not in array range
            // return the original array
            if (arr == null
                || index < 0
                || index >= arr.length) {
      
                return arr;
            }
      
            // return the resultant array
            return IntStream.range(0, arr.length)
                .filter(i -> i != index)
                .map(i -> arr[i])
                .toArray();
        }
      
        // Driver Code
        public static void main(String[] args)
        {
      
            // Get the array
            int[] arr = { 1, 2, 3, 4, 5 };
      
            // Print the resultant array
            System.out.println("Original Array: "
                               + Arrays.toString(arr));
      
            // Get the specific index
            int index = 2;
      
            // Print the index
            System.out.println("Index to be removed: "
                               + index);
      
            // Remove the element
            arr = removeTheElement(arr, index);
      
            // Print the resultant array
            System.out.println("Resultant Array: "
                               + Arrays.toString(arr));
        }
    }

    chevron_right

    
    

    Output:

    Original Array: [1, 2, 3, 4, 5]
    Index to be removed: 2
    Resultant Array: [1, 2, 4, 5]
    
  3. Using ArrayList:

    Approach:

    1. Get the array and the index.
    2. Form an ArrayList with the array elements.
    3. Remove the specified index element using remove() method.
    4. Form a new array of the ArrayList using mapToInt() and toArray() methods.
    5. Return the formed array.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to remove an element
    // from a specific index from an array
      
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
      
    class GFG {
      
        // Function to remove the element
        public static int[] removeTheElement(int[] arr, 
                                               int index)
        {
      
            // If the array is empty
            // or the index is not in array range
            // return the original array
            if (arr == null
                || index < 0
                || index >= arr.length) {
      
                return arr;
            }
      
            // Create ArrayList from the array
            List<Integer> arrayList = IntStream.of(arr)
                                          .boxed()
                                          .collect(Collectors.toList());
      
            // Remove the specified element
            arrayList.remove(index);
      
            // return the resultant array
            return arrayList.stream()
                .mapToInt(Integer::intValue)
                .toArray();
        }
      
        // Driver Code
        public static void main(String[] args)
        {
            // Get the array
            int[] arr = { 1, 2, 3, 4, 5 };
      
            // Print the resultant array
            System.out.println("Original Array: "
                               + Arrays.toString(arr));
      
            // Get the specific index
            int index = 2;
      
            // Print the index
            System.out.println("Index to be removed: "
                               + index);
      
            // Remove the element
            arr = removeTheElement(arr, index);
      
            // Print the resultant array
            System.out.println("Resultant Array: "
                               + Arrays.toString(arr));
        }
    }

    chevron_right

    
    

    Output:

    Original Array: [1, 2, 3, 4, 5]
    Index to be removed: 2
    Resultant Array: [1, 2, 4, 5]
    
  4. Using System.arraycopy():

    Approach:

    1. Get the array and the index.
    2. Create a new array of size one less than the size of original array.
    3. Copy the elements from starting till index from original array to the other array using System.arraycopy()
    4. Copy the elements from index + 1 till end from original array to the other array using System.arraycopy()
    5. Return the formed array.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to remove an element
    // from a specific index from an array
      
    import java.util.Arrays;
      
    class GFG {
      
        // Function to remove the element
        public static int[] removeTheElement(int[] arr, 
                                               int index)
        {
      
            // If the array is empty
            // or the index is not in array range
            // return the original array
            if (arr == null
                || index < 0
                || index >= arr.length) {
      
                return arr;
            }
      
            // Create another array of size one less
            int[] anotherArray = new int[arr.length - 1];
      
            // Copy the elements from starting till index
            // from original array to the other array
            System.arraycopy(arr, 0, anotherArray, 0, index);
      
            // Copy the elements from index + 1 till end
            // from original array to the other array
            System.arraycopy(arr, index + 1,
                             anotherArray, index,
                             arr.length - index - 1);
      
            // return the resultant array
            return anotherArray;
        }
      
        // Driver Code
        public static void main(String[] args)
        {
            // Get the array
            int[] arr = { 1, 2, 3, 4, 5 };
      
            // Print the resultant array
            System.out.println("Original Array: "
                               + Arrays.toString(arr));
      
            // Get the specific index
            int index = 2;
      
            // Print the index
            System.out.println("Index to be removed: "
                               + index);
      
            // Remove the element
            arr = removeTheElement(arr, index);
      
            // Print the resultant array
            System.out.println("Resultant Array: "
                               + Arrays.toString(arr));
        }
    }

    chevron_right

    
    

    Output:

    Original Array: [1, 2, 3, 4, 5]
    Index to be removed: 2
    Resultant Array: [1, 2, 4, 5]
    


My Personal Notes arrow_drop_up

Technical Content Engineer

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.