Skip to content

Commit 341ffc7

Browse files
authored
Update 283-Move-Zeroes.js
Updated the code as suggested.
1 parent a9a42da commit 341ffc7

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

Diff for: javascript/283-Move-Zeroes.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@
77
*/
88
var moveZeroes = function(nums) {
99

10-
const zeroAtTheEnd = Array(nums.length).fill(0);
11-
let left = 0;
12-
for (let i = 0; i < nums.length; i++) {
13-
const isNonZero = nums[i];
14-
if (isNonZero) {
15-
zeroAtTheEnd[left] = nums[i];
10+
const arr = new Array(nums.length).fill(0);
11+
12+
let [left, right] = [0, 0];
13+
14+
while (right < nums.length) {
15+
const isZero = (nums[right] === 0);
16+
if (!isZero) {
17+
arr[left] = nums[right];
1618
left++;
1719
}
20+
21+
right++;
1822
}
19-
return zeroAtTheEnd;
23+
24+
return arr;
2025
};
2126

2227
/**

0 commit comments

Comments
 (0)