Skip to content

Commit

Permalink
Create 26-Remove-Duplicates-from-Sorted-Array.js
Browse files Browse the repository at this point in the history
Solved remove-duplicates-from-sorted-array with JS.
  • Loading branch information
aadil42 committed Dec 25, 2022
1 parent 7144043 commit c8d54f7
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions javascript/26-Remove-Duplicates-from-Sorted-Array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Linear
* Time O(N) | Space O(1)
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {

let left = 0;
let right = 1;
let delCount = 0;

while (right <= nums.length) {
if (nums[left] === nums[right]) {
right++;
delCount++;
} else {
nums.splice(left + 1, delCount);
delCount = 0;
left++;
right = left + 1;
}
}
};

0 comments on commit c8d54f7

Please sign in to comment.