Problems › Arrays & strings

Move Zeroes

Difficulty: Easy · Topic: Arrays & strings

Given an array nums, return a new array with all 0s moved to the end while keeping the relative order of the non-zero elements.

Examples

move_zeroes([0,1,0,3,12])
→ [1,3,12,0,0]

Non-zero order kept; two zeroes trail.

move_zeroes([0])
→ [0]

Target complexity: time O(n), space O(n)

Write your solution in Python and run it against hidden tests right on this page. Progressive hints and Python + C++ reference solutions are included.

Learn the idea first: the lesson for this problem.

More Arrays & strings problems