DEV Community

bengineer
bengineer

Posted on

LeetCode 283: Move Zeroes (Ruby)

Description: https://leetcode.com/problems/move-zeroes/

Solution:

def move_zeroes(nums)
        zeros = nums.count(0)
    nums.delete(0)
    zeros.times do
        nums << 0
    end
    nums
end
Enter fullscreen mode Exit fullscreen mode

Explanation:

1- Count how many zeros there's in the array.

2- Delete all zeros from the original array.

3- Using the counter from step 1, add zeros to the end of the array.

4- Return the array.

Top comments (0)