Move Zeros to the End (Set 11)

Easy

Given an array of integers nums, return a new array with every 0 pushed to the end while the relative order of the non-zero values stays the same.

Move Zeros to the End (Set 11) — the data. This set runs 4 cases: nums holds arrays of 44–48 items, with values from 0 to 5. The hidden cases are the largest of the set.

Example 1

moveZerosToEnd()
Input
nums = [0, 5, 4, 0, 1, 4, 2, 2, 1, 1, 4, 5, 2, 3, 5, 2, 0, 5, 5, 4, 3, 4, 4, 1, 2, 2, 1, 4, 3, 4, 1, 4, 0, 3, 0, 2, 5, 3, 5, 4, 4, 1, 5, 0, 0, 1]
Output
[5, 4, 1, 4, 2, 2, 1, 1, 4, 5, 2, 3, 5, 2, 5, 5, 4, 3, 4, 4, 1, 2, 2, 1, 4, 3, 4, 1, 4, 3, 2, 5, 3, 5, 4, 4, 1, 5, 1, 0, 0, 0, 0, 0, 0, 0]

Why

`moveZerosToEnd` returns `[5, 4, 1, 4, 2, 2, 1, 1, 4, 5, 2, 3, 5, 2, 5, 5, 4, 3, 4, 4, 1, 2, 2, 1, 4, 3, 4, 1, 4, 3, 2, 5, 3, 5, 4, 4, 1, 5, 1, 0, 0, 0, 0, 0, 0, 0]` for this input.

Example 2

moveZerosToEnd()
Input
nums = [0, 4, 2, 5, 4, 5, 5, 3, 4, 0, 3, 5, 5, 5, 5, 3, 1, 5, 4, 2, 0, 1, 3, 5, 1, 4, 0, 0, 0, 1, 3, 4, 1, 3, 0, 5, 5, 5, 4, 4, 4, 4, 5, 3]
Output
[4, 2, 5, 4, 5, 5, 3, 4, 3, 5, 5, 5, 5, 3, 1, 5, 4, 2, 1, 3, 5, 1, 4, 1, 3, 4, 1, 3, 5, 5, 5, 4, 4, 4, 4, 5, 3, 0, 0, 0, 0, 0, 0, 0]

Why

`moveZerosToEnd` returns `[4, 2, 5, 4, 5, 5, 3, 4, 3, 5, 5, 5, 5, 3, 1, 5, 4, 2, 1, 3, 5, 1, 4, 1, 3, 4, 1, 3, 5, 5, 5, 4, 4, 4, 4, 5, 3, 0, 0, 0, 0, 0, 0, 0]` for this input.
Constraints
0 <= nums.length <= 10^4
-10^4 <= nums[i] <= 10^4
Every case in this set must pass: the first two are visible on Run, the last two unlock on Submit.

Topics

ArraysTwo Pointers

Implement moveZerosToEnd in the editor and return the answer — printing it is never graded.