Skip to content

Commit c8d54f7

Browse files
authored
Create 26-Remove-Duplicates-from-Sorted-Array.js
Solved remove-duplicates-from-sorted-array with JS.
1 parent 7144043 commit c8d54f7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Linear
3+
* Time O(N) | Space O(1)
4+
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var removeDuplicates = function(nums) {
9+
10+
let left = 0;
11+
let right = 1;
12+
let delCount = 0;
13+
14+
while (right <= nums.length) {
15+
if (nums[left] === nums[right]) {
16+
right++;
17+
delCount++;
18+
} else {
19+
nums.splice(left + 1, delCount);
20+
delCount = 0;
21+
left++;
22+
right = left + 1;
23+
}
24+
}
25+
};

0 commit comments

Comments
 (0)