Remove Repeated Values from a Sorted Array

Implement removeDuplicates

Given an array nums sorted in non-decreasing order, return a new array containing only its unique values, keeping their original relative order. The array being sorted is the key hint here — it means every duplicate of a value sits right next to it, so you never need to search back through everything you've already collected.

Example 1:

Input: nums = [1,1,2,3,3,3,5]

Output: [1,2,3,5]

Example 2:

Input: nums = [4,4,4,4]

Output: [4]

Example 3:

Input: nums = [2,5,9]

Output: [2,5,9]

+ 3 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 10⁵
  • -10⁹ ≤ nums[i] ≤ 10⁹
  • nums is sorted in non-decreasing order

nums =

[1, 1, 2, 3, 3, 3, 5]