Given an array of a 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 }
An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be quickly sorted or searched. All the items of the array are stored at contiguous memory locations.
Approaches
There are numerous approaches to check whether a specific element is present in this Array or not in Java. These are –
- Using another Array
- Using Java 8 Streams
- Using ArrayList
- Using System.arraycopy() method
1. Using Another Array (Naive or Basic approach)
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
// 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)); } } |
Original Array: [1, 2, 3, 4, 5] Index to be removed: 2 Resultant Array: [1, 2, 4, 5]
2. 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 the 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
// 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)); } } |
Original Array: [1, 2, 3, 4, 5] Index to be removed: 2 Resultant Array: [1, 2, 4, 5]
3. 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
// 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)); } } |
Original Array: [1, 2, 3, 4, 5] Index to be removed: 2 Resultant Array: [1, 2, 4, 5]
4. Using System.arraycopy() method
Approach:
- Get the array and the index.
- Create a new array of size one less than the size of the original array.
- Copy the elements from starting till index from the original array to the other array using System.arraycopy().
- Copy the elements from index + 1 till the end from the original array to the other array using System.arraycopy().
- Return the formed array.
Below is the implementation of the above approach.
Java
// 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)); } } |
Original Array: [1, 2, 3, 4, 5] Index to be removed: 2 Resultant Array: [1, 2, 4, 5]
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!