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 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));
}
}
Output: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 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));
}
}
Output: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 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));
}
}
Output: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 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));
}
}
Output:Original Array: [1, 2, 3, 4, 5] Index to be removed: 2 Resultant Array: [1, 2, 4, 5]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.