Remove Duplicates From Sorted Array II
Shorten a sorted array in place so that each value appears at most twice.
You are given an array of integers numsnumskknums
Example 1
Output: 5, nums = [1, 1, 2, 2, 3, _]
Example 2
Output: 7, nums = [0, 0, 1, 1, 2, 3, 3, _, _]
The Idea
Think of a sorted pile of tickets, each with a number, where you may keep at most two tickets of each number. You go through the pile from the top, and you throw away a ticket only when you already hold two with the same number.
Example 1 works the same way. Reading [1, 1, 1, 2, 2, 3]_
The kept values have to end up at the front of the same array. So as we read along, we copy each value we keep to the next free slot at the front. The front part then always holds the values kept so far, in sorted order. How do we decide, for the value we are reading, that it would be a third copy?
Call the value being read xknums[0]nums[k - 1]nums[k]
In the top row, two 1s are already kept in slots 0 and 1, so kxnums[k - 2]nums[k - 1]x
In the bottom row, xnums[k - 2]nums[k]k
So one comparison is enough: x
Solution Approach
Turning that idea into code means settling three things: where the kept values are written, how to spot a third copy, and what to do with the first two values.
Where the kept values go
Why not simply delete each extra copy where it stands? Deleting from the middle of an array means shifting every later value one slot to the left. With many extra copies, that shifting happens again and again, and the work grows with the square of the length.
Writing to the front avoids all shifting. The code keeps two positions in the same array: the read position, which moves over every value, and the write position kk
Spotting a third copy
Which value should xnums[k - 2]
The first two values
What about the start, when fewer than two values are kept? Then nums[k - 2]kk < 2nums[k - 2]
As an optional extension, the same loop allows any limit. Replace 2 with a number m in both places in the check, and each value is kept at most m times.
Java Solution
The method removeDuplicates
- First, set
to 0. It counts the values kept so far, and it is also the slot where the next kept value goes.k - Read the array from left to right with an enhanced
loop, which hands out each value in turn asfor .x - For each
, checkx . When it holds, writek < 2 || x != nums[k - 2] intox and add one tonums[k] . Otherwisek would be a third copy, and the loop moves on.x - When the loop is done, return
.k
The maink
import java.util.Arrays;
public class RemoveDuplicatesFromSortedArrayII {
public static int removeDuplicates(int[] nums) {
int k = 0; // nums[0..k-1] holds the values kept so far
for (int x : nums) {
if (k < 2 || x != nums[k - 2]) { // not a third copy of x
nums[k] = x;
k++;
}
}
return k;
}
static void run(int[] nums) {
int k = removeDuplicates(nums);
System.out.println("k = " + k + ", kept: " + Arrays.toString(Arrays.copyOf(nums, k))
+ ", whole array: " + Arrays.toString(nums));
}
public static void main(String[] args) {
run(new int[] {1, 1, 1, 2, 2, 3});
run(new int[] {0, 0, 1, 1, 1, 1, 2, 3, 3});
run(new int[] {2, 2, 2, 2, 2});
run(new int[] {1, 2, 3});
run(new int[] {7});
run(new int[] {});
}
}
Running it prints:
k = 5, kept: [1, 1, 2, 2, 3], whole array: [1, 1, 2, 2, 3, 3]
k = 7, kept: [0, 0, 1, 1, 2, 3, 3], whole array: [0, 0, 1, 1, 2, 3, 3, 3, 3]
k = 2, kept: [2, 2], whole array: [2, 2, 2, 2, 2]
k = 3, kept: [1, 2, 3], whole array: [1, 2, 3]
k = 1, kept: [7], whole array: [7]
k = 0, kept: [], whole array: []
Look at the first line: k = 5[1, 1, 2, 2, 3]3, 3_[0, 0, 1, 1, 2, 3, 3, 3, 3]
The run of five 2s gives k = 2k[1, 2, 3]k < 2nums[k - 2]
What if the check used the input position instead, nums[i] != nums[i - 2]i[1, 1, 1, 2, 2, 3][1, 1, 2, 3]
The original implementation,
Complexity
The work is a single pass over the array, with one comparison per value.
- Time: O(n), for an array of n numbers, because the loop reads each value once and does one comparison and at most one write for it.
- Space: O(1). The kept values are written over the front of
, and the only extra variable isnums .k
With 1 in place of 2 in the check, the same loop keeps each value once: see