Move Zeroes

Easy#283same direction

You get an array of numbers. Move every zero to the end and keep the other numbers in the order they were in. Do it inside the same array, without a second one. Our example: [0, 4, 0, 0, 7, 2] → [4, 7, 2, 0, 0, 0].

input
nums: integers, some of them may be 0.
returns
Nothing: change nums in place so that all the zeros are at the end and the other numbers keep their order before them. Do not copy the array.
constraints
  • 1 ≤ nums.length ≤ 10⁴
  • −2³¹ ≤ nums[i] ≤ 2³¹ − 1
our 13 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
function moveZeroes(nums) {

}
Ctrl ↵
format
reset

saved in this browser

13 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more two pointers problems

Pattern “Two pointers” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.