Keep One of Each

EasyTwo Pointers≈ 10 min

You are given a list of integers nums, sorted from smallest to largest, in which some values may repeat. Return the list with one copy of each value, still in order.

Examples

text
nums = [1, 1, 2, 3, 3, 3, 7]   →  [1, 2, 3, 7]
nums = [4, 4, 4]               →  [4]
nums = [-2, 0, 5]              →  [-2, 0, 5]

Constraints

  • Between 0 and 10,000 numbers, sorted, each between -1,000,000,000 and 1,000,000,000.
  • Aim for O(n) time and O(1) memory beyond the answer: the list is sorted, so a set is not needed.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 3 visible cases here, in your browser. Submit runs all 7 on the judge, 4 of them hidden, and measures how your solution scales.