Introduction
The Remove Element problem involves removing all occurrences of a given value from an array.
Given an array of integers and a value val, the task is to remove every occurrence of val in-place and return the number of remaining elements.
This problem helps in understanding:
- Array traversal
- In-place modification
- Two Pointer Technique
- Element filtering
Problem Statement
Given an integer array nums and an integer val, remove all occurrences of val from the array in-place.
Return the number of elements that are not equal to val.
The order of the remaining elements does not need to be preserved.
Example
Input:nums = [3, 2, 2, 3]
val = 3
Output:
2
Array after removal:
[2, 2]
Explanation:
The value 3 occurs twice, so both occurrences are removed.
The remaining elements are [2, 2].
Input:nums = [0, 1, 2, 2, 3, 0, 4, 2]
val = 2
Output:
5
Array after removal:
[0, 1, 3, 0, 4]
Explanation:
All occurrences of 2 are removed.
There are 5 remaining elements.
Constraints
- 0 ≤ nums.length ≤ 100
- 0 ≤ nums[i] ≤ 50
- 0 ≤ val ≤ 100
Approach 1: Brute Force
Explanation
The brute force approach uses an extra array to store only the elements that are not equal to val.
We traverse the original array and add every valid element to the result array.
This approach is simple to understand but requires extra space.
Steps
- Create an empty result array.
- Traverse the given array.
- If the current element is not equal to val, add it to the result array.
- Return the size of the result array.
Dry Run
Input:nums = [3, 2, 2, 3]
val = 3
Traverse the array:
3 → Skip
2 → Add → [2]
2 → Add → [2, 2]
3 → Skip
Final Result:
[2, 2]
Number of remaining elements:
2
Brute Force Code
Complexity Analysis
Time Complexity: O(n)
Every element is checked once.
Space Complexity: O(n)
An extra array is used to store the remaining elements.
Approach 2: Optimized Solution
Explanation
The optimized approach uses the Two Pointer Technique and modifies the array in-place.
We use one pointer to traverse the array and another pointer to indicate where the next valid element should be placed.
Whenever an element is not equal to val, place it at the position indicated by the second pointer.
Steps
- Initialize k = 0.
- Traverse every element of the array.
- If the current element is not equal to val:
- Store it at nums[k].
- Increment k.
- Return k.
Dry Run
Input:nums = [3, 2, 2, 3]
val = 3
Initially:
k = 0
3 → Skip
2 → nums[0] = 2
k = 1
2 → nums[1] = 2
k = 2
3 → Skip
Final Array:
[2, 2, 2, 3]
Only the first 2 elements are valid.
Output:
2
Optimized Code
Complexity Analysis
Time Complexity: O(n)
Each element is traversed exactly once.
Space Complexity: O(1)
The array is modified in-place without using an extra array.
Edge Cases
- Empty array: No elements need to be removed.
- No occurrence of val: The original array remains unchanged.
- All elements equal val: The result length is 0.
- Single element: The result depends on whether the element equals val.
- Duplicate values: Every occurrence of val is removed.
Why This Problem is Important
This problem helps in understanding:
- Array traversal
- In-place modification
- Two Pointer Technique
- Element filtering
- Space optimization
Real-World Applications
The same filtering concept can be useful in:
- Data cleaning
- Removing unwanted records
- Filtering datasets
- Memory-efficient array processing
- Input preprocessing
Common Mistakes
- Creating an unnecessary extra array in the optimized solution.
- Forgetting to increment k after placing a valid element.
- Returning the array length instead of the number of valid elements.
- Modifying the wrong array position.
- Processing only the first occurrence of val instead of all occurrences.
Interview Tips
The important requirement in this problem is in-place modification.
For an optimized solution, explain the Two Pointer Technique clearly:
Traverse the array → keep valid elements at the front → return the count of valid elements.
The key idea is that elements after the returned length do not matter.
Related Questions
- Move Zeroes
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Move Zeroes
- Sort Colors
Final Takeaway
The Remove Element problem teaches how to filter an array while modifying it in-place.
The brute force approach uses an extra array, while the optimized Two Pointer approach achieves O(n) time and O(1) extra space.