Write an algorithm that takes an array and moves all of the zeros to the end JavaScript

We need to write a function that moves all zeros in an array to the end while maintaining the order of non-zero elements. This algorithm should work in-place without using extra space.

Problem Overview

Given an array with mixed numbers including zeros, we want to push all zeros to the end while keeping other elements in their relative order. For example, [1, 0, 3, 0, 5] should become [1, 3, 5, 0, 0].

Method 1: Using splice() and push()

This approach removes zeros when found and adds them to the end:

const arr = [34, 6, 76, 0, 0, 343, 90, 0, 32, 0, 34, 21, 54];

const moveZero = (arr) => {
    for(let ind = 0; ind < arr.length; ind++){
        const el = arr[ind];
        if(el === 0){
            arr.push(arr.splice(ind, 1)[0]);
            ind--;
        }
    }
};

moveZero(arr);
console.log(arr);
[34, 6, 76, 343, 90, 32, 34, 21, 54, 0, 0, 0, 0]

Method 2: Two-Pointer Approach (More Efficient)

A more efficient solution uses two pointers to avoid repeated array modifications:

const moveZerosEfficient = (arr) => {
    let writeIndex = 0;
    
    // Move all non-zero elements to the front
    for(let readIndex = 0; readIndex < arr.length; readIndex++) {
        if(arr[readIndex] !== 0) {
            arr[writeIndex] = arr[readIndex];
            writeIndex++;
        }
    }
    
    // Fill remaining positions with zeros
    while(writeIndex < arr.length) {
        arr[writeIndex] = 0;
        writeIndex++;
    }
};

const testArray = [1, 0, 3, 0, 5, 0, 7];
console.log("Original:", testArray);
moveZerosEfficient(testArray);
console.log("Result:", testArray);
Original: [1, 0, 3, 0, 5, 0, 7]
Result: [1, 3, 5, 7, 0, 0, 0]

How It Works

The two-pointer approach maintains two indices:

  • writeIndex: Points to where the next non-zero element should be placed
  • readIndex: Scans through the array looking for non-zero elements

Comparison

Method Time Complexity Space Complexity Array Operations
splice() + push() O(n²) O(1) Many (expensive)
Two-pointer O(n) O(1) Minimal (efficient)

Complete Example with Edge Cases

const moveZeros = (arr) => {
    let writeIndex = 0;
    
    for(let readIndex = 0; readIndex < arr.length; readIndex++) {
        if(arr[readIndex] !== 0) {
            arr[writeIndex] = arr[readIndex];
            writeIndex++;
        }
    }
    
    while(writeIndex < arr.length) {
        arr[writeIndex] = 0;
        writeIndex++;
    }
    
    return arr;
};

// Test different scenarios
console.log("Mixed array:", moveZeros([1, 0, 2, 0, 3]));
console.log("All zeros:", moveZeros([0, 0, 0]));
console.log("No zeros:", moveZeros([1, 2, 3]));
console.log("Single element:", moveZeros([0]));
Mixed array: [1, 2, 3, 0, 0]
All zeros: [0, 0, 0]
No zeros: [1, 2, 3]
Single element: [0]

Conclusion

The two-pointer approach is the optimal solution with O(n) time complexity. It efficiently moves zeros to the end while preserving the order of non-zero elements in a single pass.

Updated on: 2026-03-15T23:18:59+05:30

353 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements