JavaScript Program to Move Zeros to End of Array
Use a loop to collect non-zero elements and then add zeros at the end, like this:
function moveZeros(arr) { let result = arr.filter(x => x !== 0); let zeros = arr.length - result.length; return result.concat(Array(zeros).fill(0)); }Examples
Input[0, 1, 0, 3, 12]
Output[1, 3, 12, 0, 0]
Input[1, 2, 3, 4]
Output[1, 2, 3, 4]
Input[0, 0, 0]
Output[0, 0, 0]
How to Think About It
Think of separating the array into two parts: all the numbers that are not zero, and all the zeros. First, collect all non-zero numbers in order, then add all zeros at the end. This keeps the order of non-zero numbers and moves zeros to the end.
Algorithm
1
Get the input array.2
Create a new list with all elements that are not zero, keeping their order.3
Count how many zeros were in the original array.4
Add that many zeros to the end of the new list.5
Return the new list.Code
javascript
function moveZeros(arr) { const nonZeros = arr.filter(num => num !== 0); const zeroCount = arr.length - nonZeros.length; const zeros = Array(zeroCount).fill(0); return nonZeros.concat(zeros); } const input = [0, 1, 0, 3, 12]; const output = moveZeros(input); console.log(output);
Output
[1, 3, 12, 0, 0]
Dry Run
Let's trace [0, 1, 0, 3, 12] through the code
1
Filter non-zero elements
nonZeros = [1, 3, 12]
2
Count zeros
zeroCount = 5 - 3 = 2
3
Create zeros array
zeros = [0, 0]
4
Concatenate arrays
result = [1, 3, 12].concat([0, 0]) = [1, 3, 12, 0, 0]
| Step | nonZeros | zeroCount | zeros | result |
|---|---|---|---|---|
| 1 | [1, 3, 12] | |||
| 2 | 2 | |||
| 3 | [0, 0] | |||
| 4 | [1, 3, 12, 0, 0] |
Why This Works
Step 1: Filter non-zero elements
Using filter keeps all numbers except zeros, preserving their order.
Step 2: Count zeros
Subtracting the length of non-zero elements from original length gives the number of zeros.
Step 3: Add zeros at the end
Creating an array of zeros and concatenating it moves all zeros to the end.
Alternative Approaches
In-place swapping
javascript
function moveZerosInPlace(arr) { let insertPos = 0; for (let i = 0; i < arr.length; i++) { if (arr[i] !== 0) { [arr[insertPos], arr[i]] = [arr[i], arr[insertPos]]; insertPos++; } } return arr; } console.log(moveZerosInPlace([0,1,0,3,12]));
Modifies the original array without extra space, but changes input directly.
Using reduce
javascript
function moveZerosReduce(arr) { const result = arr.reduce((acc, val) => { if (val !== 0) acc.push(val); else acc.zeros++; return acc; }, []); return result.concat(Array(result.zeros || 0).fill(0)); } console.log(moveZerosReduce([0,1,0,3,12]));
Uses reduce to separate zeros and non-zeros, slightly more complex but functional style.
Complexity: O(n) time, O(n) space
Time Complexity
The code loops through the array once to filter non-zero elements, so it runs in linear time O(n).
Space Complexity
It creates new arrays for non-zero elements and zeros, so it uses O(n) extra space.
Which Approach is Fastest?
The in-place swapping method is fastest in space (O(1)) but modifies the original array, while filter + concat is simpler but uses extra space.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Filter + concat | O(n) | O(n) | Simplicity and preserving original array |
| In-place swapping | O(n) | O(1) | Memory efficiency and modifying original array |
| Reduce method | O(n) | O(n) | Functional style and clarity |
Use
filter to separate non-zero elements easily before adding zeros at the end.Forgetting to preserve the order of non-zero elements or modifying the array incorrectly.