27. Remove Element - Detailed Explanation
Problem Statement
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed. After removing, return the new length of the array containing only the elements that are not equal to val.
Important:
It does not matter what values are set beyond the returned length.
Examples
Example 1:
Input: nums = [3,2,2,3], val = 3
Output: 2, with nums = [2,2,_,_]
Explanation:
After removing all occurrences of 3, the array might become [2,2,_,_] (where the underscores denote values that can be ignored). The new length is 2.
Example 2:
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, with nums = [0,1,3,0,4,_,_,_]
Explanation:
After removing all occurrences of 2, the array might become [0,1,3,0,4,_,_,_] in any order, and the new length is 5.
Example 3:
Input: nums = [1,1,1,1], val = 1
Output: 0, with nums = [_,_,_,_]
Explanation:
Every element equals val, so after removal, there are no elements left. The new length is 0.
Constraints
0 <= nums.length <= 1000 <= nums[i] <= 500 <= val <= 100
Hints
-
In-Place Requirement:
You must modify the array without using extra space for another array. This means you have to rearrange the existing array elements. -
Two-Pointer Technique:
Consider using two pointers (or indices): one to scan through the array and another to mark the position where the next non-valelement should be placed. -
Order Does Not Matter:
Since the order of the remaining elements can be changed, you only need to ensure that the first part of the array (up to the returned length) contains the non-valelements.
Approaches
1. Brute Force Approach
Idea:
Iterate through the array and, each time you find an element equal to val, shift all subsequent elements to the left by one position. Continue this until the end of the array.
Why it’s inefficient:
- In the worst case (where nearly every element is
val), every removal could require shifting many elements. - This leads to an overall time complexity of O(n²), which is acceptable for small arrays but inefficient as
ngrows.
2. Optimal Approach: Two-Pointer Technique
Idea:
Use two pointers:
- Slow pointer (
i): This pointer keeps track of the position in the array where the next valid (non-val) element should be placed. - Fast pointer (
j): This pointer iterates through every element of the array.
Procedure:
- Initialize
i = 0. - For each index
jin the array:- If
nums[j]is not equal toval, copynums[j]tonums[i]and then incrementi.
- If
- After processing,
iwill represent the new length of the array with all the elements not equal tovalin the firstipositions.
Example Walkthrough (using Example 2):
For nums = [0,1,2,2,3,0,4,2] and val = 2:
-
Start:
i = 0j = 0→nums[0] = 0is not2→ assignnums[0] = 0, incrementito1.
-
Next:
j = 1→nums[1] = 1is not2→ assignnums[1] = 1, incrementito2.
-
j = 2:
nums[2] = 2equalsval→ do nothing,iremains2.
-
j = 3:
nums[3] = 2equalsval→ do nothing,iremains2.
-
j = 4:
nums[4] = 3is not2→ assignnums[2] = 3(overwriting a2), incrementito3.
-
j = 5:
nums[5] = 0is not2→ assignnums[3] = 0, incrementito4.
-
j = 6:
nums[6] = 4is not2→ assignnums[4] = 4, incrementito5.
-
j = 7:
nums[7] = 2equalsval→ do nothing.
After the loop, i = 5. The first 5 elements of nums now contain the valid elements [0, 1, 3, 0, 4].
Complexity:
-
Time: O(n) since we traverse the array once.
-
Space: O(1) since only constant extra space is used.
Code Implementations
Python Implementation
Java Implementation
Complexity Analysis
-
Time Complexity:
Both implementations use a single loop to traverse the array. Thus, the time complexity is O(n), wherenis the length ofnums. -
Space Complexity:
The algorithm uses constant extra space (only a few integer variables), so the space complexity is O(1).
Common Mistakes
-
Not Modifying the Array In-Place:
A common error is to create a new array to store the valid elements. The problem specifically requires modifying the original array in-place. -
Off-by-One Errors:
Be careful with the pointer indices, especially when copying elements over. -
Incorrectly Handling the Case When All Elements Are Removed:
When all elements equalval, the function should return0.
Edge Cases
-
Empty Array:
Ifnumsis empty, the output should be0since there are no elements to process. -
No Occurrence of
val:
If the array does not containval, the new length should equal the original length and the array should remain unchanged. -
All Elements Are
val:
If every element is equal toval, the returned length should be0.
Alternative Variations & Related Problems
-
Remove Duplicates from Sorted Array:
A similar two-pointer technique is used in the problem where you remove duplicates from a sorted array. -
Move Zeroes:
Another array in-place problem where you move all zeroes to the end while maintaining the order of non-zero elements. -
Partition Array Problems:
Problems where you partition an array based on a condition (such as separating even and odd numbers) often use a similar in-place technique.
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78