LeetCode #26 Easy

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 k slots are checked. Whatever sits after them does not matter.
  • No second array: the extra memory must be O(1).
  • nums has at least one element.
Constraints
  • 1 <= nums.length <= 3 * 10⁴
  • -100 <= nums[i] <= 100
  • nums is sorted in non-decreasing order.
arraytwo-pointers
Open on LeetCode ↗
02

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.

03

Approach

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def removeDuplicates(self, nums: List[int]) -> int:
▶3 k = 1
▶4 for i in range(1, len(nums)):
▶5 if nums[i] != nums[k - 1]:
▶6 nums[k] = nums[i]
▶7 k += 1
▶8 return k
0011122334writereadfirst value 0 is always kept
k1slots kept
i1
Nothing comes before 0, so it cannot be a repeat. The kept part starts as slot 0, and the next new value will be written to slot 1.
0011122334writeread0 equals last kept 0 → skip it
nums[i]0
last kept0slot 0
k1
0 matches the last kept value. Sorted order puts every copy of 0 in one run, so this is a repeat and the read index moves on without writing.
0011122334writeread1 differs from last kept 0 → keep it
nums[i]1
last kept0slot 0
k1
1 is not the last value kept, and because the array is sorted no older kept value can equal it either. It is new.
0111122334writeread1 copied into slot 1 → 2 values kept
nums[1]1
k2
Slot 1 held an old repeat that has already been read, so it is safe to overwrite. The kept part grows to 2 values.
0111122334writeread1 equals last kept 1 → skip it
nums[i]1
last kept1slot 1
k2
1 matches the last kept value. Sorted order puts every copy of 1 in one run, so this is a repeat and the read index moves on without writing.
0111122334writeread1 equals last kept 1 → skip it
nums[i]1
last kept1slot 1
k2
1 matches the last kept value. Sorted order puts every copy of 1 in one run, so this is a repeat and the read index moves on without writing.
0111122334writeread2 differs from last kept 1 → keep it
nums[i]2
last kept1slot 1
k2
2 is not the last value kept, and because the array is sorted no older kept value can equal it either. It is new.
0121122334writeread2 copied into slot 2 → 3 values kept
nums[2]2
k3
Slot 2 held an old repeat that has already been read, so it is safe to overwrite. The kept part grows to 3 values.
0121122334writeread2 equals last kept 2 → skip it
nums[i]2
last kept2slot 2
k3
2 matches the last kept value. Sorted order puts every copy of 2 in one run, so this is a repeat and the read index moves on without writing.
0121122334writeread3 differs from last kept 2 → keep it
nums[i]3
last kept2slot 2
k3
3 is not the last value kept, and because the array is sorted no older kept value can equal it either. It is new.
0123122334writeread3 copied into slot 3 → 4 values kept
nums[3]3
k4
Slot 3 held an old repeat that has already been read, so it is safe to overwrite. The kept part grows to 4 values.
0123122334writeread3 equals last kept 3 → skip it
nums[i]3
last kept3slot 3
k4
3 matches the last kept value. Sorted order puts every copy of 3 in one run, so this is a repeat and the read index moves on without writing.
0123122334writeread4 differs from last kept 3 → keep it
nums[i]4
last kept3slot 3
k4
4 is not the last value kept, and because the array is sorted no older kept value can equal it either. It is new.
0123422334write4 copied into slot 4 → 5 values kept
nums[4]4
k5
Slot 4 held an old repeat that has already been read, so it is safe to overwrite. The kept part grows to 5 values.
0123422334return 5: slots 0 to 4 hold each value once
k5
kept0, 1, 2, 3, 4
Every value has been read. The answer is 5. The faded slots still hold old numbers; the judge never looks at them.
05

Common pitfalls

Building a new list in Python

✗ Wrong
nums = sorted(set(nums))
return len(nums)
✓ Right
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

✗ Wrong
for i in range(1, len(nums)):
    if nums[i] == nums[i - 1]:
        del nums[i]
✓ Right
if nums[i] != nums[k - 1]:
    nums[k] = nums[i]
    k += 1

Each 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²).

06

Edge cases

Every value the same, e.g. [5, 5, 5]

Every comparison matches nums[0], nothing is copied, and the answer is 1.

No duplicates, e.g. [1, 2, 3]

Every value is new and k always equals i, so each value is copied onto itself and the answer is the full length.

07

Complexity

Time
O(n)
Space
O(1)
Each element is read once and written at most once. Only the counter k and the loop index are extra.
08

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.