← All Patterns

Pattern Recognition Drill

#126

Easy Two Pointers

The Problem

Given a sorted array, remove duplicates in-place and return the new length.

What approach would you use?

Think about it before scrolling down.

Remove Duplicates from Sorted Array

Key Signals

Two Pointers

Slow pointer writes unique values. Fast pointer scans. When different, advance slow and copy. O(n).

Alt: Pointer Walk

Same idea with a write index. Every unique element gets written to the next position.

Common Trap

Don't shift the entire array when removing — just overwrite with the slow pointer.

← #125 #127 →