Practice/Meta/Leetcode 26. Remove Duplicates from Sorted Array
CodingMust
You are given an integer array sorted in non-decreasing order. Your task is to modify the array in-place by moving all unique elements to the front of the array, preserving their relative order. After this operation, return the count of unique elements.
The array should be modified such that the first k positions contain each unique element exactly once in sorted order, where k is the number of distinct values. The contents of the array beyond position k do not matter.
You must accomplish this using only constant extra space (in-place modification).
Example 1:
Input: nums = [1, 1, 2, 2, 3, 4, 4, 5] Output: 5 Explanation: After modification, nums = [1, 2, 3, 4, 5, _, _, _] where _ represents don't-care values. The function returns 5.
Example 2:
Input: nums = [7, 7, 7, 7] Output: 1 Explanation: Only one unique element exists. After modification, nums = [7, _, _, _] and the function returns 1.
Example 3:
Input: nums = [1, 2, 3, 4, 5] Output: 5 Explanation: All elements are already unique. The array remains unchanged and the function returns 5.