Skip to content

Commit ee5b5d4

Browse files
authored
Merge pull request #3705 from Rach1507/patch-1
Refactor 0026-remove-duplicates-from-sorted-array.js
2 parents 9a27b95 + 9be0363 commit ee5b5d4

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
/**
2-
* Linear
2+
* Linear
33
* Time O(N) | Space O(1)
44
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/
55
* @param {number[]} nums
66
* @return {number}
77
*/
8-
var removeDuplicates = (nums) => {
9-
let [left, right] = [0, 0];
8+
var removeDuplicates = function (nums) {
9+
let swap = 1;
1010

11-
while (right < nums.length) {
12-
const [leftVal, rightVal] = [nums[left], nums[right]];
13-
14-
const isEqual = (rightVal === leftVal);
15-
if (!isEqual) {
16-
left++;
17-
nums[left] = rightVal;
18-
}
19-
20-
right++;
11+
for (let i = 1; i < nums.length; i++) {
12+
if (nums[i] != nums[i - 1]) {
13+
nums[swap] = nums[i];
14+
swap++;
2115
}
22-
23-
return (left + 1);
16+
}
17+
return swap;
2418
};

0 commit comments

Comments
 (0)