Remove Duplicates from a Sorted Array

Given a nonempty integer array nums sorted in non-decreasing order, remove duplicates in-place and return the number k of distinct values. The first k…

Given a nonempty integer array nums sorted in non-decreasing order, remove duplicates in-place and return the number k of distinct values. The first k elements of the original nums must contain each distinct value once in increasing order. Values beyond that prefix are unspecified; do not return a replacement array.

- 1 <= nums.length <= 100000 - -10000 <= nums[i] <= 10000 - nums is sorted in non-decreasing order. - The teaching trace accepts at most 12 values; coding retains the full bounds.

Example 1: input nums = [1,1,2]; output k = 2, prefix = [1,2]. Write 1 and 2 into the first two positions and return 2. The remaining suffix is unspecified.

Example 2: input nums = [0,0,1,1,1,2,2,3,3,4]; output k = 5, prefix = [0,1,2,3,4]. Each of the five distinct values appears once in the valid prefix.

Category: Two Pointers. Topic: two-pointers. Difficulty: Easy. Patterns: two-pointers.