Notice Period

Dedupe the Sorted Stream

medium

A sensor uploads its readings in sorted order, but flaky radio links make it resend values — the log arrives sorted with runs of duplicates. Storage is tight on the base station: clean the log in place, no second array.

Given the sorted list log, remove the duplicates in place so each value appears once (order preserved), and return k, the number of unique values. After your function runs, the first k slots of log must hold the unique values in order; what's beyond them doesn't matter.

Return the cleaned prefix log[:k] as the function result.

Example 1
in log = [1, 1, 2, 2, 2, 3]
out [1, 2, 3]
Three unique readings; the first 3 slots become [1, 2, 3].
Example 2
in log = [4]
out [4]
Constraints
  • · 1 <= len(log) <= 10^5
  • · -10^9 <= log[i] <= 10^9
  • · log is sorted non-decreasing
  • · O(1) extra space
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.