easyArrayTwo PointersTwo Pointers

Remove Duplicates from Sorted Array

easyTime: O(n)Space: O(1)

Recognize the pattern

sorted arrayremove duplicates in-placereturn new length

Brute force idea

If you approach Remove Duplicates from Sorted Array in the most literal way possible, you get this: Create new array with unique elements —. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

Better approach

The deeper shift in Remove Duplicates from Sorted Array is this: Two pointers: slow marks unique position, fast scans for new values. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

Key invariant

At the center of Remove Duplicates from Sorted Array is one steady idea: Elements before slow pointer are all unique and in sorted order. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Watch out for

A common way to get lost in Remove Duplicates from Sorted Array is this: Returning the wrong value — return slow + 1 (the count), not slow (the index). Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Two Pointers Pattern