Problem Number: 27 Difficulty: Easy Category: Array, Two Pointers LeetCode Link: https://leetcode.com/problems/remove-element/
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.
Since it is impossible to change the length of the array in some programming languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
Return k after placing the final result in the first k slots of nums.
Example 1:
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,3,0,4,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the order of those five elements can be arbitrary.
Constraints:
0 <= nums.length <= 1000 <= nums[i] <= 500 <= val <= 100
I used a Two Pointer approach with in-place modification. The key insight is to use a slow pointer to track the position where the next non-target element should be placed, and a fast pointer to scan through the array.
Algorithm:
- Initialize slow pointer k at index 0
- Iterate through array with fast pointer i
- When nums[i] != val, copy nums[i] to nums[k] and increment k
- Skip elements equal to val (don't copy them)
- Return k (number of elements not equal to val)
The solution uses a two-pointer approach for in-place element removal. See the implementation in the solution file.
Key Points:
- Uses two pointers: slow pointer (k) and fast pointer (i)
- Modifies array in-place without extra space
- Copies only non-target elements to the front
- Returns count of elements not equal to val
- Maintains relative order of non-target elements
Time Complexity: O(n)
- We traverse the array once with the fast pointer
- Each element is checked and potentially copied once
- Total: O(n)
Space Complexity: O(1)
- Uses only a constant amount of extra space
- Modifies the array in-place
- No additional data structures needed
-
Two Pointer Technique: Using a slow pointer to track the next position for non-target elements and a fast pointer to scan through the array.
-
In-Place Modification: The problem requires modifying the array in-place, which is efficiently done with two pointers.
-
Selective Copying: We only copy elements that are not equal to the target value, effectively removing all occurrences.
-
Order Preservation: The two-pointer approach preserves the relative order of non-target elements.
-
Efficient Skipping: We skip target elements without copying them, minimizing unnecessary operations.
-
Return Value: The function returns the count of elements not equal to the target value.
-
Extra Space Usage: Initially might use a new array or list to store non-target elements.
-
Wrong Pointer Logic: Not properly understanding when to increment the slow pointer.
-
Inefficient Approach: Using remove() or similar methods that shift elements.
-
Return Value Confusion: Returning the wrong value or not understanding what k represents.
- Remove Duplicates from Sorted Array (Problem 26): Remove duplicates in sorted array
- Move Zeroes (Problem 283): Move all zeros to the end while maintaining order
- Sort Colors (Problem 75): Sort array with only 0, 1, 2 values
- Remove Duplicates from Sorted Array II (Problem 80): Allow at most 2 occurrences
- New Array: Create a new array with non-target elements (violates in-place requirement)
- List Methods: Use remove() method (inefficient due to shifting)
- Filter: Use filter functions (violates in-place requirement)
- Extra Space: Using additional data structures when in-place modification is required.
- Wrong Pointer Logic: Not understanding when to move the slow pointer.
- Inefficient Methods: Using methods that shift elements or create new arrays.
- Return Value: Not understanding what the return value represents.
- Order Preservation: Not maintaining the relative order of non-target elements.
Note: This is a fundamental two-pointer problem that demonstrates efficient in-place element removal.