Move Zeroes to the End of Array
Easy
262
10
55.4% Acceptance
In this lab, you will be building a function that takes an integer array as input and moves all the 0
s present in the array to the end while maintaining the relative order of the non-zero elements. You are required to implement this function in-place without making a copy of the input array.
Here's your task:
- Implement the
moveZeroes
function that takes an integer arraynums
as input and moves all0
s present in the array to the end while maintaining the relative order of the non-zero elements.
Consider the following example:
Example 1:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Example 2:
Input: nums = [1,0,0,4,5]
Output: [1,4,5,0,0]
Example 3:
Input: nums = [0]
Output: [0]
Challenges
- Export the
moveZeroes
function. - Test the
moveZeroes
function with an array of integers and validate if all the zeroes are moved to the end of the array while maintaining the relative order of the non-zero elements.