Max Zander

Posted on

# Solving Leetcode's "Move Zeroes" with JavaScript

So before I even get into my solution for how I solved Leetcode's "Move Zeroes" problem, I just want to mention something super obvious. ALWAYS READ YOUR INSTRUCTIONS!! Now, you may be saying to yourself, "But Max, of course one reads the instructions! Why would you say something so obvious?!", but let me tell you that part of growing is learning from your mistakes. Going to solve this problem, I first read the instructions and then looked at the examples:

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]

Constraints:

• 1 <= nums.length <= 104
• -231 <= nums[i] <= 231 - 1

So what did I do? After looking at the examples, I saw [1,3,12,0,0] as sorted in order with the zeroes moved to the end and so my first few times around, I sorted my nums array before moving the zeroes!

So yeah, moral of the story: ALWAYS READ YOUR INSTRUCTIONS! In fact, I would recommend, reading the instructions, looking at the examples, and then reading the instructions again! Because even if you look at it quickly and say to yourself, "Oh! That's an easy one!", you very well might have missed something.

Ok so, sidebar aside, let's talk about how to solve the actual problem!

So let's look at the problem again:

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]

Constraints:

• 1 <= nums.length <= 104
• -231 <= nums[i] <= 231 - 1

So what's the goal? We are given an array called nums and have to take that array and leave everything as is except for any zeroes (0), which we move to the end of the array. All this while not making a copy of the array.

So first things first, we want to create a variable to keep track of non-zeroes. Let's call it "nonZero":

var moveZeroes = function(nums) {
let nonZero = 0;
};

Cool. So now that we have our nonZero variable, let's loop through the nums array to look for non-zeroes. We can accomplish this with a simple for loop. If the current element in our array is not equal to zero (0), we can set it to the current index of nonZero and then increment nonZero to keep going:

var moveZeroes = function(nums) {
let nonZero = 0;
for(let i=0; i < nums.length; i++){
if(nums[i] !== 0){
nums[nonZero] = nums[i];
nonZero++;
};
};

Now, to take care of our zeroes, we're going to use another loop.

NB: This is a separate, non-nested loop, which will ultimately keep our solution to O(2n) time complexity, which since constants don't matter in Big O expressions, can just be simplified to **O(n)* time complexity!*

This loop is going to go through the array at the nonZero index and replace those elements with the appropriate zeroes and it looks a little something like this:

for(let i = nonZero; i < nums.length; i++) {
nums[i] = 0;
};

All together, the solution looks like this:

var moveZeroes = function(nums) {
let nonZero = 0;
for(let i=0; i < nums.length; i++){
if(nums[i] !== 0){
nums[nonZero] = nums[i];
nonZero++;
};
};

for(let i = nonZero; i < nums.length; i++) {
nums[i] = 0;
};
};

But again, perhaps the most important thing to take away from this post is to please READ YOUR INSTRUCTIONS MORE THAN ONCE because it will save you SO much time and complexity! (See what I did there? ðŸ˜‚)