Move Zeroes in JavaScript 🎉

Move Zeroes in JavaScript 🎉

The journey of learning and solving Data Structures and Algorithm questions is very interesting and challenging but I love the fact that I am learning and getting better daily.

In this article you will learn how to solve the Move Zeroes Question on Leetcode in a way that is easy to implement.

Let's get in✨

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

Example 1:

  • Input: nums = [0,1,0,3,12]
  • Output: [1,3,12,0,0]

Example 2:

  • Input: nums = [0]
  • Output: [0]

Solution (Logic)

Let's solve this question in few steps below🎉

  • Get the length of the nums array and store it in a variable called len.
 // Length of array
    len = nums.length;
  • Do some checks on the input array called nums, I call it sanity check😂. Check if the value is not valid and the length is equal or less than 1, if it satisfies this condition then we can return the nums array.
      // Sanity Checks
    if (!nums || len <= 1) return nums;
  • The next step is to iterate through the nums array using a for loop.

  • During iteration, we will use the JavaScript splice method to cut the value of the array that is equal to zero by its index.

 for (let i = 0; i < len; i++) {
        if(nums[i] == 0) {
            nums.splice(i, 1);
        }
    }
  • After cutting away the zero, since our aim is to push all the zeroes to the back of the array then we can use the JavaScript push method to add a zero to the back of the array immediately we see a zero.
 for (let i = 0; i < len; i++) {
        if(nums[i] == 0) {
            nums.splice(i, 1);
            nums.push(0);
        }
    }
  • We have now achieved our main goal of pushing all zeroes to the back but the length of nums array increases after adding zero to the back, we need to decrease the length and index during iteration so that the loop can just be for the initial values of the array and not with the newly added zeroes.
    for (let i = 0; i < len; i++) {
          if(nums[i] == 0) {
              nums.splice(i, 1);
              nums.push(0);
              len--;
              i--;
          }
      }
    
  • We are done with the moving of all zeroes found in an array to the back of the array in just few steps✌

The Time and Space Complexity

Time Complexity

This Solution has a Time complexity of O(n) which is a Linear time since we are looping through the nums array and it depends on the length of the array.

Space complexity

The Space Complexity is also O(n) since we are using extra space to store the values in an array.

The Runtime is 116ms, faster than 28.09% of JavaScript online submissions for Move Zeroes and Memory Usage: 43.5 MB, less than 14.07% of JavaScript online submissions for Move Zeroes.

Move Zeroes Solution.JPG

Conclusion

I hope you have learnt a lot from this article and your contribution is also highly welcomed because we get better by learning from others, you can reach out to me on Twitter here . Watch out for more helpful contents on Data Structures and Algorithm from me.

Don't forget to Like, share and subscribe for more articles in this series called Solutions to Data Structures and Algorithm Questions on Leetcode.

Enjoy 🎉