Move Zeros to the End (Set 8)

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 8) — the data. This set runs 4 cases: nums holds arrays of 32–46 items, with values from 0 to 5. The hidden cases are the largest of the set.

Example 1

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

Why

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

Example 2

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

Why

`moveZerosToEnd` returns `[1, 2, 3, 1, 2, 5, 2, 5, 5, 5, 4, 3, 5, 3, 4, 5, 2, 5, 2, 4, 3, 5, 5, 1, 3, 2, 5, 0, 0, 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.