-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoveZerosToEnd.js
More file actions
executable file
·41 lines (28 loc) · 864 Bytes
/
moveZerosToEnd.js
File metadata and controls
executable file
·41 lines (28 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// move zeros to the end of the array
// pseudocode
// count = 0;
// for i from 0 to n-1:
// if arr[i] != 0:
// swap(arr[i], arr[count])
// count++;
// return arr;
function moveZerosToEnd(arr) {
if (!Array.isArray(arr)) throw new TypeError("Input must be an array");
let index = 0;
for (let num of arr) {
if (num !== 0) {
[arr[index], num] = [num, arr[index]];
index++;
}
}
while (index < arr.length) {
arr[index++] = 0;
}
return arr;
}
console.log(moveZerosToEnd([0, 1, 0, 3, 12])); // [1,3,12,0,0]
console.log(moveZerosToEnd([0, 0, 0, 0])); // [0,0,0,0]
console.log(moveZerosToEnd([1, 2, 3, 4])); // [1,2,3,4]
console.log(moveZerosToEnd([4, 0, 5, 0, 6, 0, 7])); // [4,5,6,7,0,0,0]
console.log(moveZerosToEnd([])); // []
console.log(moveZerosToEnd([9, 0, 8, 0, 7, 0, 6])); // [9,8,7,6,0,0,0]