Implement in-place duplicate removal
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in in-place array manipulation, handling duplicates in sorted sequences, and formal reasoning about space and time complexity.
Remove Duplicates from Sorted Array (In-Place)
Constraints
- 0 <= len(nums) <= 3 * 10^4
- -10^4 <= nums[i] <= 10^4
- nums is sorted in non-decreasing order
- Must modify nums in place with O(1) extra memory
Examples
Input: ([1, 1, 2],)
Expected Output: 2
Explanation: Unique prefix becomes [1, 2]; new length is 2.
Input: ([0, 0, 1, 1, 1, 2, 2, 3, 3, 4],)
Expected Output: 5
Explanation: Distinct values [0,1,2,3,4]; new length is 5.
Input: ([],)
Expected Output: 0
Explanation: Empty array has no unique values; length 0.
Input: ([5],)
Expected Output: 1
Explanation: Single element is already unique; length 1.
Input: ([2, 2, 2, 2],)
Expected Output: 1
Explanation: All identical collapse to a single 2; length 1.
Input: ([-3, -3, -1, 0, 0, 7],)
Expected Output: 4
Explanation: Distinct values [-3,-1,0,7]; length 4 (handles negatives).
Input: ([1, 2, 3, 4, 5],)
Expected Output: 5
Explanation: Already all distinct; length unchanged at 5.
Hints
- Keep one pointer for the position of the last unique value already written, and a second pointer that scans forward.
- Because the array is sorted, all copies of a value are contiguous — you only need to compare each element with the most recently written one.
- When nums[read] differs from nums[write - 1], write it at index `write` and increment `write`.
Remove Duplicates from Sorted Array II — At Most Twice (In-Place)
Constraints
- 0 <= len(nums) <= 3 * 10^4
- -10^4 <= nums[i] <= 10^4
- nums is sorted in non-decreasing order
- Each distinct value may appear at most twice in the result
- Must modify nums in place with O(1) extra memory
Examples
Input: ([1, 1, 1, 2, 2, 3],)
Expected Output: 5
Explanation: Third 1 is dropped; result prefix [1,1,2,2,3]; length 5.
Input: ([0, 0, 1, 1, 1, 1, 2, 3, 3],)
Expected Output: 7
Explanation: 1 appears 4 times -> kept twice; result [0,0,1,1,2,3,3]; length 7.
Input: ([],)
Expected Output: 0
Explanation: Empty array; length 0.
Input: ([4],)
Expected Output: 1
Explanation: Single element kept; length 1.
Input: ([2, 2, 2, 2],)
Expected Output: 2
Explanation: Four 2s collapse to two; length 2.
Input: ([1, 2, 3],)
Expected Output: 3
Explanation: All distinct, none exceeds twice; length 3.
Input: ([-1, -1, -1, 0, 0],)
Expected Output: 4
Explanation: Third -1 dropped; result [-1,-1,0,0]; length 4 (handles negatives).
Hints
- Always keep the first two elements of any equal-value block, then drop the rest.
- Compare the current candidate against the element two slots before the write pointer (nums[write - 2]) instead of one slot back.
- Guard with `write < 2` so the first two writes are unconditional; this pattern generalizes to 'at most k copies' by comparing with nums[write - k].