Removing Duplicates from Sorted Array II Using Two Pointers
Problem Statement
Given a sorted integer array nums, remove duplicates in-place sothat each unique element appears no more than twice. Return the new length of the array.
Key constraints:
- Must modify the input array in-place
- Space complexity must be O(1)
- The function receives the array by reference
Examples:
Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3]
Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3]
Solusion Approach
This problem extends the classic "remove duplicates" challenge by allowing each element to appear at most twice instead of once. The two-pointer technique provides an elegant solution.
The core strategy involves two indices:
read_ptrscans through the original arraywrite_ptrmarks the position where the next valid element should be written
The critical insight is checking whether placing an element would create a third occurrence. By comparing the current element with nums[write_ptr - 2], we determine if this would exceed the two-occurrence limit.
If nums[read_ptr] != nums[write_ptr - 2], the element is valid and gets written to position write_ptr, which then increments.
Implementation
def remove_extra_duplicates(arr):
"""
Remove duplicates in-place, keeping at most two occurrences.
Returns the new length of the modified array.
"""
if len(arr) <= 2:
return len(arr)
write_idx = 2
for read_idx in range(2, len(arr)):
if arr[read_idx] != arr[write_idx - 2]:
arr[write_idx] = arr[read_idx]
write_idx += 1
return write_idx
# Test cases
def test_solution():
test1 = [1, 1, 1, 2, 2, 3]
len1 = remove_extra_duplicates(test1)
print(f"Length: {len1}, Array: {test1[:len1]}")
test2 = [0, 0, 1, 1, 1, 1, 2, 3, 3]
len2 = remove_extra_duplicates(test2)
print(f"Length: {len2}, Array: {test2[:len2]}")
test_solution()
Output:
Length: 5, Array: [1, 1, 2, 2, 3]
Length: 7, Array: [0, 0, 1, 1, 2, 3, 3]
Complexity Analysis
| Metric | Value | Explanation |
|---|---|---|
| Time | O(n) | Single pass through the array |
| Space | O(1) | Only two integer variables used |
Key Points
The algorithm maintains the relative order of elements while performing in-place modification. The pointer write_idx always points to the next available slot for writing, and elements beyond the returned length should be ignored by the caller.
For arrays with 2 or fewer elements, the function returns immediately since no duplicates can exist beyond the allowed threshold.