How to Remove a Specific Element from an Array in Java
Removing an element from an array is a common programming task, but Java arrays have one important limitation—they are fixed in size. Unlike collections such as ArrayList, you cannot directly delete an element from an array or reduce its length.
To remove an element from an array, you typically create a new array without the unwanted element. Alternatively, if frequent insertions and removals are required, using an ArrayList is usually the better choice.
In this tutorial, you'll learn how to remove an element from a Java array, remove all occurrences of a value, understand the difference between removing by index and by value, and discover the best approach for different situations.
Problem Statement
Given the following array:
int[] numbers = {10, 20, 30, 40, 50};
Remove the element 30 (or the element at index 2).
Before
[10, 20, 30, 40, 50]
After
[10, 20, 40, 50]
The Fixed-Size Array Challenge
Arrays in Java have a fixed length.
Once created, their size cannot be increased or decreased.
This means "removing" an element actually involves one of two approaches:
- Creating a new, smaller array and copying all required elements.
- Using a dynamic collection such as
ArrayList, which automatically handles resizing.
Method 1: Shift-Left Technique (Pure Arrays)
The most common array solution is to create a new array that is one element smaller and copy every element except the one being removed.
Example
import java.util.Arrays;
public class Main {
public static int[] removeElementByIndex(int[] arr, int indexToRemove) {
int[] result = new int[arr.length - 1];
int resultIndex = 0;
for (int i = 0; i < arr.length; i++) {
if (i != indexToRemove) {
result[resultIndex++] = arr[i];
}
}
return result;
}
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
numbers = removeElementByIndex(numbers, 2);
System.out.println(Arrays.toString(numbers));
}
}
Output
[10, 20, 40, 50]
Explanation
The algorithm:
- Creates a new array with one fewer element.
- Copies every element except the one at the specified index.
- Returns the new array.
Time Complexity: O(n)
Space Complexity: O(n)
Method 2: Using ArrayList
If elements need to be removed frequently, ArrayList provides a much simpler solution.
Example
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
List<Integer> list = new ArrayList<>();
for (int num : numbers) {
list.add(num);
}
list.remove(Integer.valueOf(30));
System.out.println(list);
}
}
Output
[10, 20, 40, 50]
Explanation
ArrayList automatically shifts the remaining elements after removal.
You do not need to manually create another array.
Removing All Occurrences of a Value
Sometimes the same value appears multiple times.
Instead of removing only the first occurrence, remove every matching value.
Example
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 30, 20, 30, 40, 30};
int target = 30;
int[] result = new int[numbers.length];
int index = 0;
for (int num : numbers) {
if (num != target) {
result[index++] = num;
}
}
result = Arrays.copyOf(result, index);
System.out.println(Arrays.toString(result));
}
}
Output
[10, 20, 40]
Explanation
Every occurrence of the target value is skipped while copying.
Finally, Arrays.copyOf() trims the unused portion of the array.
Removing by Index vs Removing by Value
This is one of the most common sources of confusion with ArrayList<Integer>.
Remove by Index
list.remove(1);
Removes the element at index 1.
Example:
[10, 20, 30]
↓
[10, 30]
Remove by Value
list.remove(Integer.valueOf(20));
Removes the value 20, regardless of its position.
This works because Integer.valueOf() forces Java to use the remove(Object) method instead of remove(int index).
Step-by-Step Explanation
Consider:
[10, 20, 30, 40, 50]
Remove index:
2
Step 1
Copy:
10
Result:
[10]
Step 2
Copy:
20
Result:
[10, 20]
Step 3
Skip:
30
Step 4
Copy:
40
Result:
[10, 20, 40]
Step 5
Copy:
50
Final result:
[10, 20, 40, 50]
Internal Working
Original array:
[10, 20, 30, 40, 50]
New array:
[10, 20, 40, 50]
The removed element is simply skipped while copying.
Since arrays cannot shrink, a brand-new array stores the final result.
Real-Life Analogy
Imagine five people standing in a queue.
A B C D E
If person C leaves the queue, everyone behind moves forward one position.
The queue becomes:
A B D E
The same idea applies when removing an element from an array—every remaining element shifts left to fill the gap.
Best Practices
- Use
ArrayListwhen frequent insertions and removals are required. - Use arrays only when a fixed-size structure is necessary.
- Clearly distinguish between removing by index and removing by value.
- Use
Arrays.copyOf()after removing multiple occurrences. - Handle cases where the specified index or value does not exist.
Common Mistakes
1. Confusing remove by Index and remove by Value
Incorrect:
list.remove(2);
This removes the element at index 2, not the value 2.
Correct:
list.remove(Integer.valueOf(2));
2. Assuming Arrays Can Shrink
Arrays always keep their original length.
A new array must be created after removal.
3. Ignoring the "Value Not Found" Case
Always verify that the target value or index exists before attempting removal.
4. Removing Elements One at a Time
If multiple values must be removed, perform a single traversal instead of repeatedly creating new arrays.
Expert Tips
- The distinction between
remove(int)andremove(Object)is a popular Java interview question. - When removing many elements, use one traversal rather than repeatedly rebuilding arrays.
- If removals are common throughout your program,
ArrayListis generally a better choice than arrays. - Understanding how arrays and
ArrayListdiffer helps avoid many common Java programming mistakes.
Comparison Table
| Method | Output Type | Handles Multiple Occurrences | Simplicity |
|---|---|---|---|
| Shift-Left Technique | New Array | ❌ No (One at a Time) | Medium |
| Shift-Left (Single Pass) | New Array | ✅ Yes | Medium |
ArrayList remove() |
List | ✅ Yes (Using Loop or removeIf()) |
High |
Frequently Asked Questions
1. Can I remove an element from an array without creating another array?
No. Java arrays have a fixed size, so removing an element requires creating a new array or using a dynamic collection such as ArrayList.
2. What is the difference between list.remove(2) and list.remove(Integer.valueOf(2))?
list.remove(2) removes the element at index 2.
list.remove(Integer.valueOf(2)) removes the value 2 if it exists.
3. How do I remove every occurrence of a value?
Traverse the array once, copy only the required elements, and trim the result using Arrays.copyOf().
4. Is ArrayList better than arrays for removal operations?
Yes. ArrayList automatically shifts elements and resizes itself, making removal much easier.
5. What happens if the value is not present?
For ArrayList, removing by value simply returns false and leaves the list unchanged.
For arrays, you should explicitly check whether the value exists before creating a new array.
6. Can I use removeIf() with ArrayList?
Yes.
For example:
list.removeIf(num -> num == target);
This removes every matching element from the list.
7. How do I remove an element from a 2D array?
Typically, you remove an entire row or column by creating a new array and copying the required rows or columns.
8. Is removing multiple elements at once more efficient?
Yes. Removing all matching elements in a single traversal is much more efficient than repeatedly removing one element at a time.