Remove Duplicates from Sorted Array
Remove Duplicates from Sorted Array is LeetCode 26 (Easy). You get an integer array nums sorted in non-decreasing order. Change it in place so each distinct value appears once, in the original order, and return k, the number of distinct values.
- Only the first
kslots are checked. Whatever sits after them does not matter. - No second array: the extra memory must be O(1).
numshas at least one element.
- 1 <= nums.length <= 3 * 10⁴
- -100 <= nums[i] <= 100
- nums is sorted in non-decreasing order.
Intuition
Because the array is sorted, equal values sit next to each other in one run. A value is new exactly when it differs from the last value you kept; there is no need to remember anything older.
Keep a write index k. Read left to right, and copy a value to slot k only when it differs from nums[k - 1], the last value kept.
The write index never passes the read index, so a copy only lands on a slot that has already been read. Nothing you still need is overwritten.
Approach
Keep the first value
Set k = 1. The first element has nothing before it, so it is always new, and the array is never empty.
Read every other value once
For i from 1 to the end, compare nums[i] with nums[k - 1]. If they are equal, nums[i] belongs to a run you already have, so skip it.
Copy a new value to slot k
When nums[i] differs, write it to nums[k] and add 1 to k. The front of the array now holds every distinct value seen so far, in order.
Return k
After the last read, nums[0..k-1] are the distinct values and k is their count. The slots after them keep stale numbers, which the judge ignores.
Code in Python, C++ and Java
Common pitfalls
Building a new list in Python
nums = sorted(set(nums)) return len(nums)
nums[k] = nums[i] # write into the same list
nums = … only rebinds the local name. The caller's list is untouched, so for [1, 1, 2] the judge still reads [1, 1] in the first two slots. A new list also breaks the O(1) memory rule.
Deleting inside the loop
for i in range(1, len(nums)):
if nums[i] == nums[i - 1]:
del nums[i]if nums[i] != nums[k - 1]:
nums[k] = nums[i]
k += 1Each del shifts every later element left, so the loop skips the next value and runs past the shrunken end: [1, 1, 1] raises IndexError. Even when it works, the shifting costs O(n²).
Edge cases
Every comparison matches nums[0], nothing is copied, and the answer is 1.
Every value is new and k always equals i, so each value is copied onto itself and the answer is the full length.
Complexity
k and the loop index are extra.Remove Duplicates from Sorted Array FAQ
Why does this only work on a sorted array?
The check looks only at the last kept value. In an unsorted array like [1, 2, 1], the second 1 is compared with 2, looks new, and is kept twice. Sorting puts every copy of a value in one run, so one look back is enough.
What if each value may appear up to twice?
Then compare with the value two places back in the kept part instead of one. That variant is Remove Duplicates from Sorted Array II.
Why don't the leftover slots need clearing?
The judge reads only nums[0..k-1]. Writing zeros or blanks after slot k costs time and changes nothing it checks.