Skip to content

Commit efca3a5

Browse files
authored
Merge pull request #2515 from alexanderjsx/main
2 parents d11684b + 374c615 commit efca3a5

File tree

1 file changed

+22
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)