Skip to content

Remove Duplicates from Sorted Array

EasyTwo Pointers
Decode it first. Clues, constraints, pattern and plan before any code.

You are given a list nums sorted in non-decreasing order. Remove the duplicates in place so each distinct value appears once, keeping their order. Return k, the number of distinct values. The first k slots of nums must hold those values; whatever is after them does not matter.

nums = [1, 1, 2]                        →  k = 2, nums starts with [1, 2]
nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]   →  k = 5, nums starts with [0, 1, 2, 3, 4]

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • -100 <= nums[i] <= 100
  • nums is sorted in non-decreasing order.
  • Use O(1) extra space.