Created
October 26, 2024 18:10
-
-
Save sAVItar02/53c2e0e7abe3721197d1a39a179289e6 to your computer and use it in GitHub Desktop.
Remove Duplicates from Sorted Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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