FunctionRacerProblemsRace

Squash Repeats in Place

EasyTwo pointersPython, JavaScript, TypeScript, Java, C++

nums is sorted in non-decreasing order. Rearrange it in place so that each distinct value appears exactly once, in increasing order, at the front of the list, and return n, the number of distinct values.

Only the first n items of nums are checked; whatever sits after them does not matter. A single pass with a read index and a write index is enough, because the write index never gets ahead of the read index.

Example 1

nums [1,1,2]
returns [1,2]

Return 2; the first two items must be [1, 2].

Example 2

nums [0,0,1,1,1,2,2,3,3,4]
returns [0,1,2,3,4]

Return 5; the first five items must be [0, 1, 2, 3, 4].

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems