-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
moving-zeros-to-the-end.js
65 lines (56 loc) · 1.51 KB
/
moving-zeros-to-the-end.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// moveZeros([false,1,0,1,2,0,1,3,"a"]) // returns[false,1,1,2,1,3,"a",0,0]
var moveZeros = function (arr) {
// a place to store the movedZeros array
const movedZeros = [];
// a place to store the zero count
let zeroCount = 0;
// iterate over the array
for (let i = 0; i < arr.length; i++) {
const value = arr[i];
// if the current value is not a zero
if (value !== 0) {
// push into movedZeros array
movedZeros.push(value);
} else {
// increment zero count
zeroCount++;
}
}
// iterate up to zero count
for (let i = 0; i < zeroCount; i++) {
// push zero into the array
movedZeros.push(0);
}
// return the movedZeros array
return movedZeros;
}
var moveZeros = function (arr) {
let zeroCount = 0;
const movedZeros = arr.reduce((movedZeros, value, index) => {
if (value !== 0) {
movedZeros[index - zeroCount] = value;
} else {
zeroCount++;
}
return movedZeros;
}, new Array(arr.length).fill(0));
return movedZeros;
}
// NOT WORKING RIGHT NOW...
var moveZeros = function (arr) {
let zeroCount = 0;
for (let i = 0; i < arr.length - zeroCount; i++) {
const value = arr[i];
if (value === 0) {
for (let j = i; j < arr.length - 1 - zeroCount; j++) {
arr[j] = arr[j + 1];
arr[arr.length - 1 - zeroCount] = 0;
}
zeroCount++;
i--;
}
}
return arr;
}
console.log(JSON.stringify(moveZeros([1, 2, 1, 0, 1, 0, 3, 0, 1, 0]))),
console.log(JSON.stringify([1, 2, 1, 1, 3, 1, 0, 0, 0, 0]))