leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 8b7e4d5c2f0eb0c9dd13bcdc395da5e9778fa1dd
parent 0cd6eafc48cd3307dc47414f8f6c307d948a566f
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Sun, 23 Apr 2023 17:30:18 -0500

Completed remove duplicates problem with js

Diffstat:
Aremove-duplicates-from-sorted-array/remove-duplicates.js | 24++++++++++++++++++++++++
1 file changed, 24 insertions(+), 0 deletions(-)

diff --git a/remove-duplicates-from-sorted-array/remove-duplicates.js b/remove-duplicates-from-sorted-array/remove-duplicates.js @@ -0,0 +1,24 @@ +//Remove duplicate values from an array and return the number of unique values. +//This algorithm has a worst case time complexity of O(n^2) where n is the length of the +//array. This is because in a worst case the array would be spliced every time thus forcing +//a mem copy of the entire array every time. +//Runtime: 128ms Beats:24.88% +//Memory: 45.6MB Beats: 9.34% + +/** + * @param {number[]} nums + * @return {number} + */ +var removeDuplicates = function(nums) { + let unique_vals = 0; + for(let i = 0 ; i < nums.length; ++i){ + if(nums[i] == nums[i + 1]){ + nums.splice(i+1 , 1); + i--; + } + else{ + unique_vals += 1; + } + } + return unique_vals; +};