← All solutions

Move Zeroes

May 31, 2025 • Go •array, two pointers, in-place • easy

Problem

  • Given an integer array nums, move all 0's to the end while maintaining the relative order of the non-zero elements.
  • Must be done in-place without making a copy.

Approach

  • 1. Loop through the array. When you find a zero, send out a second pointer to hunt for the next non-zero value.
  • 2. Once a non-zero is found, swap it with the zero.
  • 3. Keep going until you're done. The loop moves forward whether you're swapping or not.

Edge Cases

  • [0, 0, 0] → stays the same.
  • [1, 2, 3] → nothing changes.
  • [0, 1, 0, 2] → becomes [1, 2, 0, 0].

Reflections

Time performance came in around the 19th percentile, which is really back. Too many nested scans. Memory was solid, 97th percentile not that it matters much. It did the job but had no hustle. In future solutions, I’ll switch to the cleaner two-pointer method that moves non-zeros forward directly and leaves zeros behind.

Go Solution

func moveZeroes(nums []int)  {
    for i := range(nums) {
        if nums[i] == 0 {
            j := i
            for nums[j] == 0 && j < len(nums) - 1 {
                j++
            }
            nums[i], nums[j] = nums[j], nums[i]
        }
    }
}

Performance

  • Runtime beats: 19%
  • Memory beats: 97%

Complexity

  • Time: O(n²) Double nested for loop.
  • Space: O(1)
LeetCode Problem Link