Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Move Zeros to End

Working With Arraysmedium

Course · Section 11: Working With Arrays · Lecture 149: Simple Array Methods

Implement `moveZeros(arr)` returning a new array with all non-zero values in their original order followed by all the zeros.

Sample tests

Input: moveZeros([0,1,0,3,12])
Output: [1,3,12,0,0]
Input: moveZeros([0,0])
Output: [0,0]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Mutating the input in place when an immutable result is expected here.

Learning resources

  • MDN: Array
Approach & explanation (try first)

Filtering out zeros preserves order, and padding with the removed count of zeros pushes them to the end.

Loading...
⌘/Ctrl + Enter

Run your code to see results.