AlgoScope

Remove Duplicates from Sorted Array

The writer only copies a value that differs from the last one it kept.

Two pointers, algorithm, beginner
Reading 2 at index 2. It is new, so it is written to index 1.kept 0-1102122232435464758readwrite2 > 1
Reading 2 at index 2. It is new, so it is written to index 1.kept 0-1102122232435464758readwrite2 > 1

Two Pointers: Remove duplicates

Decision, step 3 of 10

2 is different from the last kept value 1, so it is written at index 1.

How two pointers works

Cost

Best
O(n)
Average
O(n)
Worst
O(n)
Space
O(1)

Taught by the same lesson

Two Pointers covers these too, in the same run.