easy

Move Zeroes

Move every zero in an array to the end while keeping the other numbers in order.

1. Define the problem

Move Zeroes

Given an integer array nums, move all 0s to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in place without making a copy of the array. Use a slow pointer that marks where the next non-zero value should go and a fast pointer that scans for non-zero values, minimizing the total number of operations.

Constraints

  • 1 ≤ nums.length ≤ 104
  • -231 ≤ numsi ≤ 231 - 1

Example

Inputnums = [0, 1, 0, 3, 12]
Output[1, 3, 12, 0, 0]

Explanation All zeros are moved to the end while 1, 3 and 12 keep their original relative order.

2. Visualize the solution

Slow pointer marks the write slot, fast pointer scans

Slow pointer marks the write slot, fast pointer scans
Statusinit

slow=0, fast=0 (value 0). Zero found — fast advances, slow stays.

What happens in this step

slow = 0, fast = 0 (value 0)
nums[fast] == 0

fast advances without swapping since this element is already zero.
Step 1 of 5

Steps to visualize

  1. Start slow and fast both at index 0.
  2. Advance fast through the array; whenever numsfast is non-zero, swap it into numsslow and move slow forward.
  3. Values already at or before slow are the non-zero values placed so far, in order.
  4. Continue until fast reaches the end of the array.
  5. Every slot from slow onward is now 0, and non-zero order was preserved.
3. Walk through the code

Walk through the code

Same walkthrough, now with the code. Press Next to move one step and watch which lines run.

Slow pointer marks the write slot, fast pointer scans
Statusinit

slow=0, fast=0 (value 0). Zero found — fast advances, slow stays.

What happens in this step

slow = 0, fast = 0 (value 0)
nums[fast] == 0

fast advances without swapping since this element is already zero.
Step 1 of 5
4. Solution

Solution

solution.tsTypeScript
function moveZeroes(nums) {
  let slow = 0;

  for (let fast = 0; fast < nums.length; fast++) {
    if (nums[fast] !== 0) {
      const temp = nums[slow];
      nums[slow] = nums[fast];
      nums[fast] = temp;
      slow++;
    }
  }

  return nums;
}
Time
O(n)
Space
O(1)
5. Test cases

Test cases

InputExpectedCovers
nums = [0, 1, 0, 3, 12][1, 3, 12, 0, 0]example from the docstring
nums = [0, 0, 0][0, 0, 0]every element is zero
nums = [1, 2, 3][1, 2, 3]no zeros present, array is unchanged
nums = [1, 2, 0, 0][1, 2, 0, 0]zeros already positioned at the end
nums = [0][0]smallest valid input, a single zero
nums = [0, 0, 1][1, 0, 0]multiple leading zeros before the only non-zero value

Keep reading

TopicDescription
Two PointersScan a sorted array or string from both ends at once to find pairs, remove duplicates, or reverse data in O(n).
Valid PalindromeCheck whether a string reads the same forwards and backwards, ignoring case and punctuation.
Merge Sorted ArrayMerge two sorted arrays into one sorted array in place.
Remove Duplicates from Sorted ArrayRemove duplicate values from a sorted array in place and return the new length.
Two Sum II - Input Array Is SortedFind two numbers in a sorted array that add up to a target value.
3SumFind every unique triplet of numbers in an array that adds up to zero.