Mutations Only: Zeroes to the End

Published by Mubashir Hassan in

Write a function that moves all the zeroes to the end of an array.

Examples

zeroesToEnd([1, 2, 0, 0, 4, 0, 5]) ➞ [1, 2, 4, 5, 0, 0, 0]

zeroesToEnd([0, 0, 2, 0, 5]) ➞ [2, 5, 0, 0, 0]

zeroesToEnd([4, 4, 5]) ➞ [4, 4, 5]

zeroesToEnd([0, 0]) ➞ [0, 0]

Notes

  • Keep the relative order of the non-zero elements the same.
Watch a quick demo on how Edabit works.