Last active
June 27, 2022 04:22
-
-
Save perjerz/cea3e1ffe30cbe5d390af3e0e784f39c to your computer and use it in GitHub Desktop.
Remove Duplicates from Sorted Array https://leetcode.com/problems/remove-duplicates-from-sorted-array/
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) { | |
const length = nums.length; | |
let i = 1; | |
let startIndex = 0; | |
let count = 0; | |
while(i < nums.length && nums[i] != undefined) { | |
if (nums[startIndex] === nums[i]) { | |
count++; | |
} else { | |
if (count) { | |
nums.splice(startIndex, count); | |
} | |
startIndex = startIndex+1; | |
i = startIndex; | |
count = 0; | |
} | |
i++; | |
} | |
if (count) { | |
nums.splice(startIndex, count); | |
} | |
return nums.length; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment