Baseline Arrays · Easy O(n) · O(1)
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.
1 <= n <= 3 * 10^4-100 <= nums[i] <= 100nums is sorted in non-decreasing orderInput: {"nums":[1,1,2]}
Output: 2
After compaction the slab begins [1, 2], so k = 2 distinct timestamps survive.
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.