Skip to content

Commit 6727ad4

Browse files
authored
Refactor 0026-remove-duplicates-from-sorted-array.js
This alternative is More concise while maintaining readability Straightforward, Easier to understand at a glance Achieves the same result with less code , with same complexity
1 parent b114d4e commit 6727ad4

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

javascript/0026-remove-duplicates-from-sorted-array.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@
55
* @param {number[]} nums
66
* @return {number}
77
*/
8-
var removeDuplicates = (nums) => {
9-
let [left, right] = [0, 0];
8+
var removeDuplicates = function(nums) {
109

11-
while (right < nums.length) {
12-
const [leftVal, rightVal] = [nums[left], nums[right]];
10+
let swap = 1;
1311

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

0 commit comments

Comments
 (0)