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 –
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
import java.util.Arrays;
class GFG {
public static int [] removeTheElement( int [] arr, int index)
{
if (arr == null || index < 0
|| index >= arr.length) {
return arr;
}
int [] anotherArray = new int [arr.length - 1 ];
for ( int i = 0 , k = 0 ; i < arr.length; i++) {
if (i == index) {
continue ;
}
anotherArray[k++] = arr[i];
}
return anotherArray;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
System.out.println( "Original Array: "
+ Arrays.toString(arr));
int index = 2 ;
System.out.println( "Index to be removed: " + index);
arr = removeTheElement(arr, index);
System.out.println( "Resultant Array: "
+ Arrays.toString(arr));
}
}
|
OutputOriginal 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
import java.util.Arrays;
import java.util.stream.IntStream;
class GFG {
public static int [] removeTheElement( int [] arr, int index)
{
if (arr == null
|| index < 0
|| index >= arr.length) {
return arr;
}
return IntStream.range( 0 , arr.length)
.filter(i -> i != index)
.map(i -> arr[i])
.toArray();
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
System.out.println( "Original Array: "
+ Arrays.toString(arr));
int index = 2 ;
System.out.println( "Index to be removed: "
+ index);
arr = removeTheElement(arr, index);
System.out.println( "Resultant Array: "
+ Arrays.toString(arr));
}
}
|
OutputOriginal 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
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class GFG {
public static int [] removeTheElement( int [] arr, int index)
{
if (arr == null
|| index < 0
|| index >= arr.length) {
return arr;
}
List<Integer> arrayList = IntStream.of(arr)
.boxed()
.collect(Collectors.toList());
arrayList.remove(index);
return arrayList.stream()
.mapToInt(Integer::intValue)
.toArray();
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
System.out.println( "Original Array: "
+ Arrays.toString(arr));
int index = 2 ;
System.out.println( "Index to be removed: "
+ index);
arr = removeTheElement(arr, index);
System.out.println( "Resultant Array: "
+ Arrays.toString(arr));
}
}
|
OutputOriginal 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
import java.util.Arrays;
class GFG {
public static int [] removeTheElement( int [] arr, int index)
{
if (arr == null
|| index < 0
|| index >= arr.length) {
return arr;
}
int [] anotherArray = new int [arr.length - 1 ];
System.arraycopy(arr, 0 , anotherArray, 0 , index);
System.arraycopy(arr, index + 1 ,
anotherArray, index,
arr.length - index - 1 );
return anotherArray;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
System.out.println( "Original Array: "
+ Arrays.toString(arr));
int index = 2 ;
System.out.println( "Index to be removed: "
+ index);
arr = removeTheElement(arr, index);
System.out.println( "Resultant Array: "
+ Arrays.toString(arr));
}
}
|
OutputOriginal Array: [1, 2, 3, 4, 5]
Index to be removed: 2
Resultant Array: [1, 2, 4, 5]