Skip to content

Commit 337e694

Browse files
authored
Update 26-Remove-Duplicates-from-Sorted-Array.js
Updated the code as suggested and. the splice method is removed.
1 parent c8d54f7 commit 337e694

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Diff for: javascript/26-Remove-Duplicates-from-Sorted-Array.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
* @param {number[]} nums
66
* @return {number}
77
*/
8-
var removeDuplicates = function(nums) {
8+
var removeDuplicates = (nums) => {
9+
let [left, right] = [0, 0];
910

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

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;
14+
const isEqual = (rightVal === leftVal);
15+
if (!isEqual) {
2116
left++;
22-
right = left + 1;
17+
nums[left] = rightVal;
2318
}
19+
20+
right++;
2421
}
22+
23+
return (left + 1);
2524
};

0 commit comments

Comments
 (0)