← DiffPush

Remove duplicates from sorted array

Baseline Arrays · Easy O(n) · O(1)

Compacting a Deduplicated Sensor Timeline

An edge gateway stores minute-stamped sensor events in a fixed-size RAM slab that is already sorted by timestamp but littered with repeated ticks. Memory is too tight to allocate a second slab, so the repeated ticks must be squeezed out in place. Report how many leading slots now hold distinct timestamps.

Input: An array nums of n integers, sorted in non-decreasing order.

Output: An integer k. The first k slots of nums must hold the distinct values in their original order; anything beyond k is ignored.

Constraints

Examples

Example 1

Input: {"nums":[1,1,2]}
Output: 2
After compaction the slab begins [1, 2], so k = 2 distinct timestamps survive.

Example 2

Input: {"nums":[0,0,1,1,1,2,2,3,3,4]}
Output: 5
The distinct values are 0,1,2,3,4 and they are packed into the first five slots.

Solve this in your browser →

Also on LeetCode ↗