Created
January 18, 2015 22:03
-
-
Save nramirez/ac73036716b6d0c382ab to your computer and use it in GitHub Desktop.
Remove Zeros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeZeros(array) { | |
// Sort "array" so that all elements with the value of zero are moved to the | |
// end of the array, while the other elements maintain order. | |
// [0, 1, 2, 0, 3] --> [1, 2, 3, 0, 0] | |
// Zero elements also maintain order in which they occurred. | |
// [0, "0", 1, 2, 3] --> [1, 2, 3, 0, "0"] | |
var length = array.length; | |
for(var i =0; length > 0; i++, length--){ | |
if(array[i] == 0){ | |
var temp = array[i]; | |
array.splice(i,1); | |
i--; | |
array[array.length] = temp; | |
} | |
} | |
// Do not use any temporary arrays or objects. Additionally, you're not able | |
// to use any Array or Object prototype methods such as .shift(), .push(), etc | |
// the correctly sorted array should be returned. | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment