Trick is to know if a number is duplicate (seeing that num for first time) in a sorted array we just need to compare with the previous element
one point l -> takes care where u r going to update
other pointer r -> takes care of going through all numbers
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
let l = 1 ;
for ( let r = 1 ; r<nums.length;r++){
if( nums[r] != nums[r-1]){
nums[l] = nums[r];
l++
}
}
return l
};
Top comments (1)
It would be helpful to mention that this is an in-place mutation. For a new array, the Set trick is pretty good. It is lovely, though.