Skip to content

Instantly share code, notes, and snippets.

@midorikocak
Created June 2, 2017 09:49
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 midorikocak/66e9ea1f7ee48753dcd4a4093f0294e0 to your computer and use it in GitHub Desktop.
Save midorikocak/66e9ea1f7ee48753dcd4a4093f0294e0 to your computer and use it in GitHub Desktop.
This is why I hate Javascript
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
if(nums.length===1) return 1;
var i=1;
var length = nums.length;
var current = nums[0];
var toDelete = [];
while(i<length){
if(nums[i]==current){
toDelete.push(i);
}
else{
current = nums[i];
}
i++;
}
while(toDelete.length) {
nums.splice(toDelete.pop(), 1);
}
return nums.length;
};
@tallpants
Copy link

Immutability is your friend.

function removeDuplicates(nums) {
  const num_set = new Set(nums);
  nums = [...num_set.values()];
  return nums;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment