Skip to content

Instantly share code, notes, and snippets.

@TechWithTy
Created October 30, 2020 21:49
Show Gist options
  • Save TechWithTy/edba85f5135bbf816623c52cd2b1fc43 to your computer and use it in GitHub Desktop.
Save TechWithTy/edba85f5135bbf816623c52cd2b1fc43 to your computer and use it in GitHub Desktop.
Remove duplicates from array javscript
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
nums.forEach((num,i) => {
if(nums[i+1] !== null && nums[i+1] == nums[i] ){
nums.splice(i, 1);
console.log(nums)
removeDuplicates(nums)
}
})
return nums.length;
};
//O(n^2)
//repassing array for no reason
//
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
if(nums.length == 0) return 0;
let i = 0;
for (let j = 1; j < nums.length; j ++){
if(nums[j] !== nums[i]){
i++;
nums[i] = nums[j];
}
}
return i + 1
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment