remove-duplicates.js (713B)
1 //Remove duplicate values from an array and return the number of unique values. 2 //This algorithm has a worst case time complexity of O(n^2) where n is the length of the 3 //array. This is because in a worst case the array would be spliced every time thus forcing 4 //a mem copy of the entire array every time. 5 //Runtime: 128ms Beats:24.88% 6 //Memory: 45.6MB Beats: 9.34% 7 8 /** 9 * @param {number[]} nums 10 * @return {number} 11 */ 12 var removeDuplicates = function(nums) { 13 let unique_vals = 0; 14 for(let i = 0 ; i < nums.length; ++i){ 15 if(nums[i] == nums[i + 1]){ 16 nums.splice(i+1 , 1); 17 i--; 18 } 19 else{ 20 unique_vals += 1; 21 } 22 } 23 return unique_vals; 24 };