Example - Move Zeroes

Move all zeroes to the end while maintaining order.

Problem: Move zeroes to end, maintain order. In place.

function moveZeroes(nums):
    write = 0
    for read from 0 to nums.length - 1:
        if nums[read] != 0:
            nums[write] = nums[read]
            write++
    for i from write to nums.length - 1:
        nums[i] = 0

Alternative: Swap non-zeros to front in single pass.

Time: O(n)O(n). Space: O(1)O(1).