Write dedupe_sorted(nums: list[int]) -> int. nums is sorted in non-decreasing order. Rearrange it in place so that its first k slots hold each distinct value once, in the original (sorted) order, and return k. What's left in nums[k:] doesn't matter, and the list's length may stay the same.
nums = [1, 1, 2, 4, 4, 4, 7]
k = dedupe_sorted(nums) # 4
nums[:k] # [1, 2, 4, 7]
dedupe_sorted([]) # 0
Constraints: 0 <= len(nums) <= 3·10^5. Use O(1) extra memory and O(n) time.
Deleting duplicates with del or list.remove shifts the tail each time, which is O(n²) and fails the large test; building a set or a new list uses O(n) extra memory, which the tests also measure.
Show hint
walk the list once while keeping a second index for where the next new value should be written. Because the list is sorted, a value is new exactly when it differs from the last one you wrote.