LeetCode WalkThru: 'MoveZeros'
Tags: javascript, beginners, codenewbie, webdev
Published: 2021-11-29 17:09:36

:cloud: Hello Everyone :cloud:

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

To get started, here is the [link](https://leetcode.com/problems/move-zeroes/) to the challenge. Pull it up on your end and let's get started. 

:cloud::cloud::cloud:
______________________________________________________

## 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.

```

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]
```

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, <strong>if</strong> an array item's value is 0, then I want to do something specifically; <strong>else</strong> I want to do something different. 

Yep -- you guessed it. We want to consider using an <strong>if else</strong> 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:

```js
function moveZeros(nums){

}
```

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

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

A <strong>for loop</strong> takes a few conditions within its parentheses. 

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

<strong>(2)</strong> "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. 

<strong>(3)</strong>    "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:

```js
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)
    }
  }
}

```

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: 

```js
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
}

```


______________________________________________________

## Testing Our Solution

Here are some test examples to try within your code:

```
Input: nums = [0, 1, 2, 0]
```

```
Input: nums = [-33, 100, 12, 0000]
```

```
Input: nums = [1, 2, 4, 66, 99]
```

```
Input: nums = [1, 'a', 4, 0, 12]
```
______________________________________________________

## Summary

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

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. 

:cloud::cloud::cloud: