Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II is LeetCode 80 (Medium). The integer array nums is sorted in non-decreasing order. Rewrite it in place so that every value appears at most twice, keep the order, and return k, the length of what is left.
- Only
nums[0..k-1]is checked; later slots can hold anything. - Extra memory must stay O(1), so no copy of the array.
- 1 <= nums.length <= 3 * 10⁴
- -10⁴ <= nums[i] <= 10⁴
- nums is sorted in non-decreasing order.
Intuition
Call the front of the array, nums[0..k-1], the kept part. It is sorted, and no value appears in it more than twice.
A value x may join the kept part unless it equals nums[k - 2], the element two slots back. If x == nums[k - 2], then sorting forces nums[k - 1] to be x too, because it sits between them. Two copies are already kept and x would be the third. If x differs, at most one copy is kept so far, so a second is allowed.
One comparison replaces a run counter, and the first two values always pass because there is nothing two slots back yet.
Approach
Start with an empty kept part
Set k = 0. The kept part is nums[0..k-1], and it grows only from the left, so it never overtakes the element being read.
Let the first two values in
While k < 2, keep every value. Any value is allowed twice, so the first two reads can never break the rule.
Compare with the slot two back
For every later value x, compare it with nums[k - 2]. Equal means two copies of x are already kept, so skip it. Different means write x to nums[k] and add 1 to k.
Return k
Once every value is read, the kept part holds each value once or twice, in sorted order, and k is its length.
Code in Python, C++ and Java
nums[0..k-1]. It starts empty and only grows from the left, so it never runs past the value being read.nums[0..k-1]. It starts empty and only grows from the left, so it never runs past the value being read.Common pitfalls
Looking two back in the input instead of the kept part
if i < 2 or nums[i] != nums[i - 2]:
nums[k] = nums[i]
k += 1if k < 2 or nums[i] != nums[k - 2]:
nums[k] = nums[i]
k += 1Writes go into the same array, so nums[i - 2] may already hold a copied value. On [1, 1, 1, 2, 2] the 2 copied into slot 2 makes the last 2 look like a third copy, and the answer is 3 instead of 4.
Checking only the previous slot
if k < 1 or x != nums[k - 1]:
if k < 2 or x != nums[k - 2]:
Looking one slot back keeps each value once, which is the stricter rule of the first version. [1, 1, 2] returns 2, but the right answer is 3.
Edge cases
Both values pass the k < 2 test, so the array is returned whole without ever reading a negative index.
The first two 4s are kept. Every later 4 equals nums[0], which is two back from k = 2, so k stays at 2.
Complexity
nums itself.Remove Duplicates from Sorted Array II FAQ
How do I allow each value up to m times instead of 2?
Replace both 2s with m: keep x when k < m or x != nums[k - m]. The same sorting argument holds, since a match m slots back means the last m kept values are all x. With m = 1 you get Remove Duplicates from Sorted Array.
Can I count the length of each run instead?
Yes. Track the current value and how many times it has been kept, and reset the count when the value changes. It gives the same result with two extra variables and one more branch.