DEV Community

Cover image for LeetCode WalkThru: 'MoveZeros'
Adriana DiPietro
Adriana DiPietro

Posted on • Updated on

LeetCode WalkThru: 'MoveZeros'

☁️ Hello Everyone ☁️

This is the second installment of my series LeetCode WalkThru. Today, we will be working from beginning to end to solve LeetCode's 'MoveZeros' problem.

To get started, here is the link to the challenge. Pull it up on your end and let's get started.

☁️☁️☁️


Breaking Down the Instructions

On the left-hand menu, LeetCode provides some instructions:

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.

Enter fullscreen mode Exit fullscreen mode

From these instructions we can deduce a few things:

  1. "nums" is an array of integers
  2. Input = array
  3. Output = array
  4. We must maintain the order of the rest of the array items whose values do not equal 0.
  5. All zeroes, from array "nums", must be at the end of the array.
  6. We cannot make a copy of the array nums.

Looking at Some Examples

Here is an example input and output provided by LeetCode also on their left-hand menu:

Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Enter fullscreen mode Exit fullscreen mode

As we can see in this example, all of the zeroes are moved to the end of the array and the order of the remaining array items (1, 3, 12) do not switch order in relation to each other. They definitely move indices in relation to the array, but they remain ordered.


How Can We Approach This?

Now that we know what output we are seeking, how can we approach this?

Personally, and I know I've said this before, when I see the data structure "array" as an input, I automatically gear to "iteration". I do so because I want access to each element of the array, whether it is a zero or not.

So, once I have iterated and retain access to each array item, I want to comb through the results to see which array item's values equal 0. And then I want to ask myself, if an array item's value is 0, then I want to do something specifically; else I want to do something different.

Yep -- you guessed it. We want to consider using an if else statement or something that promotes the same results -- a ternary, for example.

Remember this is your coding solution. Do what makes sense to you and what you like!

Finally, at the end I either want to return the modified array with all the zeros at the back or return some sort of error message in case our input does not align with the criteria.

Let's put this plan into action!


Approach + Solution

I am going to start by declaring a function called "moveZeroes" which takes in an array, "nums" as a parameter:

function moveZeros(nums){

}
Enter fullscreen mode Exit fullscreen mode

Now that we have our function foundation laid, let's also lay out the foundation for iteration:

function moveZeros(nums){
    for (let i = 0; i < nums.length; i++){

    }
}
Enter fullscreen mode Exit fullscreen mode

A for loop takes a few conditions within its parentheses.

(1) "let i = 0" -- Executed once before the execution of what is in the code block. Here we are declaring and assigning a variable "i" to the value of 0.

(2) "i < nums.length" -- Defines the condition to be considered while executing the code block -- meaning run the code block until this condition is no longer true. While the value of "i" is less than the array's length; run the code block.

(3) "i++" -- Executed (every time) after the code block has run through. Here we are incrementing "i". We are saying move onto the next array item after the execution of the previous array item.

This is the standard, and maybe the most common way to see "for loops" in JavaScript. Like I have said before, this ensures that we can access each array item of array "nums".

You may have noticed I used the term "code block" a few times above. Code block refers to the code written between the braces of the for loop. The code block enacts whatever we want to each array item.

Accordingly, for our code block we want to do a few things:

  1. Declare and assign each array item to a variable.
  2. Declare and assign each array item's index to a variable.
  3. Remove an array item's whose values are "0".
  4. Push all of the zeros to the end of the array.

Below is what this may look like:

function moveZeroes(nums){
  for (let i = 0; i < nums.length; i++){
    // Declare variable "item" to each array item
    let item = nums[i]

    // Declare variable "index" to each array item's index
    let index = nums.indexOf(item)
    // Using an if statement, pass in a condition of "item === 0"
    if (item === 0){
      // If the array item's value is equal to 0, use "splice" to remove that (1) array item at its index. 
      nums.splice(index, 1)
      // Then re-add the item to the end of the array using "push"
      nums.push(item)
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

Finally, we need something to return. Since the instructions said clearly not to create a copy of the array -- and honestly, our code does not call for that, we can just return the "nums" array.

Our final code looks something like this:

function moveZeroes(nums){
  for (let i = 0; i < nums.length; i++){
    // Declare variable "item" to each array item
    let item = nums[i]

    // Declare variable "index" to each array item's index
    let index = nums.indexOf(item)
    // Using an if statement, pass in a condition of "item === 0"
    if (item === 0){
      // If the array item's value is equal to 0, use "splice" to remove that (1) array item at its index. 
      nums.splice(index, 1)
      // Then re-add the item to the end of the array using "push"
      nums.push(item)
    }
  }
  return nums
}

Enter fullscreen mode Exit fullscreen mode

Testing Our Solution

Here are some test examples to try within your code:

Input: nums = [0, 1, 2, 0]
Enter fullscreen mode Exit fullscreen mode
Input: nums = [-33, 100, 12, 0000]
Enter fullscreen mode Exit fullscreen mode
Input: nums = [1, 2, 4, 66, 99]
Enter fullscreen mode Exit fullscreen mode
Input: nums = [1, 'a', 4, 0, 12]
Enter fullscreen mode Exit fullscreen mode

Summary

Now, remember there are many ways to attempt to solve a coding challenge. This article is highlighting just one of many.

REMEMBER:

  1. Make your code readable.
  2. Make your code scalable.
  3. Check for "edgecases".
  4. Code in a way that makes sense to you.
  5. Keep trying.
  6. Keep learning.


Thank you for reading + coding along with me. Please feel free to leave comments, questions or suggestions below. Please be kind and patient with everyone as we are always learning.

☁️☁️☁️

Top comments (2)

Collapse
 
karsonkalt profile image
Karson Kalt

Very well explained! Love the note about blocks and loops as well.

Collapse
 
am20dipi profile image
Adriana DiPietro

Thanks Karson!!!! :)