Skip to content

Instantly share code, notes, and snippets.

@perjerz
Last active June 27, 2022 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perjerz/cea3e1ffe30cbe5d390af3e0e784f39c to your computer and use it in GitHub Desktop.
Save perjerz/cea3e1ffe30cbe5d390af3e0e784f39c to your computer and use it in GitHub Desktop.
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
const length = nums.length;
let i = 1;
let startIndex = 0;
let count = 0;
while(i < nums.length && nums[i] != undefined) {
if (nums[startIndex] === nums[i]) {
count++;
} else {
if (count) {
nums.splice(startIndex, count);
}
startIndex = startIndex+1;
i = startIndex;
count = 0;
}
i++;
}
if (count) {
nums.splice(startIndex, count);
}
return nums.length;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment