Remove Duplicates from Sorted Array

Difficulty: Medium
Source: LeetCode

Description

You are given an integer array nums sorted in non-decreasing order, and you need to modify it in-place so that each distinct value appears only once in the prefix of the array.​ After the operation, you return an integer k representing how many unique values remain at the start of nums, and the first k positions should contain those unique values in their original relative order.​

Please see full description in this link

Example

Example 1:

  • Input: nums = [1, 1, 2]​
  • Output: k = 2 and nums’s first k elements become [1, 2, _] (the last position can hold any value)
  • Explanation: The unique values are 1 and 2, so they occupy the first two positions and the function returns 2.​

Code

# To be solved