commit f6e39561fb79293eb79a219f2edd193ebb5c2bd6 parent 8b7e4d5c2f0eb0c9dd13bcdc395da5e9778fa1dd Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Sun, 23 Apr 2023 17:56:44 -0500 Completed remove duplicates using two arrays Diffstat:
| A | remove-duplicates-from-sorted-array/remove-duplicatesV2.js | | | 31 | +++++++++++++++++++++++++++++++ |
1 file changed, 31 insertions(+), 0 deletions(-)
diff --git a/remove-duplicates-from-sorted-array/remove-duplicatesV2.js b/remove-duplicates-from-sorted-array/remove-duplicatesV2.js @@ -0,0 +1,31 @@ +//This is a better implementation that runs in linear time O(n). +//My other implementation required splicing of the array which reordered +//the entire array everytime a duplicate was found, but this way only +//unique values are saved to a new list then they are placed back in the +//input one. Another way to do this would be by using a set, but both +//implementations are the same in time complexity and space complexity. +//Runtime: 64ms Beats: 89.74% +//Memory: 44.6MB Beats: 68.36% + + +/** + * @param {number[]} nums + * @return {number} + */ +var removeDuplicates = function(nums) { + let l2 = [nums[0] , ]; + let vals = 1; + for(let i = 1 ; i < nums.length ; ++i){ + if(nums[i] == nums[i - 1]){ + continue; + } + else{ + l2[vals] = nums[i]; + vals += 1; + } + } + for(let i = 0 ; i < l2.length ; ++i){ + nums[i] = l2[i]; + } + return vals; +};