Problem Statement #
Given an array of sorted numbers, remove all duplicates from it. You should not use any extra space; after removing the duplicates in-place return the new length of the array.
Example 1:
Input: [2, 3, 3, 3, 6, 9, 9]
Output: 4
Explanation: The first four elements after removing the duplicates will be [2, 3, 6, 9].
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
let resPointer = 1;
for (let right = 1; right < nums.length; right ++) {
if (nums[resPointer - 1] !== nums[right]) {
nums[resPointer] = nums[right];
resPointer++;
}
}
return resPointer;
};
Top comments (0)