Created
October 30, 2020 21:49
-
-
Save TechWithTy/edba85f5135bbf816623c52cd2b1fc43 to your computer and use it in GitHub Desktop.
Remove duplicates from array javscript
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) { | |
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