R

E

A

C

T

O

xamples 

epeat

ode

pproach

ptimize

est

Remove Zeros

Problem

Given an array of elements of arbitrary length, move all elements that are zero to the end of the array. 

 

Easy, right?

Here's the catch:

  1. You may NOT use any Array.prototype methods. (pop, splice, slice, sort, etc.)
  2. You may NOT use any Object.prototype methods.
  3. You may NOT use any temporary Arrays or Objects
  4. You must preserve the order of non-zero AND zero elements. 

Example I/O

Another example: 

The Approach

  1. Instantiate a few helper variables (I'll review this in a minute).
  2. Iterate through the array.
  3. If the current element is 0 || '0'
    1. Iterate through the remaining portion of the array
    2. Swap the next element with the current element
    3. This will move the 0 or '0' to the end of the array
    4. Decrement your helper variables
  4. Return the array

One Solution

REPL of Solution - https://repl.it/BjK7

Conclusion  

Solution : https://repl.it/BjK7

Key Takeaways:

  1. Be comfortable solving problems without the built in JavaScript helper functions.
  2. Handle edge cases (0, '0', null, undefined, etc.)

Remove Zeros

By ryankdwyer

Remove Zeros

Technical interview problem on generating string permutations

  • 987