AlgoScope

Same Direction Pointers

A reader scans ahead while a writer lags, compacting the array in place.

Two pointers, algorithm, beginner
Reading 1 at index 1. It repeats 1, so it is skipped.kept 0-0101122232435464758readwrite1 = 1
Reading 1 at index 1. It repeats 1, so it is skipped.kept 0-0101122232435464758readwrite1 = 1

Two Pointers: Remove duplicates

Decision, step 2 of 10

1 equals the last kept value, so it is skipped. The writer stays put.

How two pointers works

Cost

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

Leads to

Topics that need this one first.

Taught by the same lesson

Two Pointers covers these too, in the same run.