Skip to content

Instantly share code, notes, and snippets.

@sAVItar02
Created October 26, 2024 18:10
Show Gist options
  • Save sAVItar02/53c2e0e7abe3721197d1a39a179289e6 to your computer and use it in GitHub Desktop.
Save sAVItar02/53c2e0e7abe3721197d1a39a179289e6 to your computer and use it in GitHub Desktop.
Remove Duplicates from Sorted Array
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
for(let i=0; i<nums.length - 1; i++) {
if(nums[i] === nums[i+1]) {
nums.splice(i+1, 1);
i--;
}
}
return nums.length;
};
// 2 pointers, one pointing at start and another at one ahead of start.
// if both pointer values match, remove the second one and reduce the loop iterator once to -
// account for any duplicates appearing more that once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment