Menu
CoddyTech

Remove Duplicates from Sorted Array

You get an array of integers nums sorted in non-decreasing order, so equal values sit next to each other. Return the distinct values of nums, each once, in the order they appear. For example, [2, 2, 5] gives [2, 5].

Function

removeDuplicates(nums: integer-array) → integer-array
numsinteger-array
the integers, sorted in non-decreasing order
Returnsinteger-array
the distinct values of nums, in increasing order

Constraints

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

Examples

Input
nums = [1, 1, 2, 3, 3, 3]
Output
[1, 2, 3]
Explanation
1 appears twice and 3 three times. Keeping one of each leaves [1, 2, 3].

lock icon+15 hidden tests on Submit

challenge icon

Follow-up

Can you do it with O(1) extra memory, by rewriting nums in place instead of building a second array?

Reset code
def removeDuplicates(nums):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

nums = [1, 1, 2, 3, 3, 3]

Expected

[1, 2, 3]