Skip to content

Instantly share code, notes, and snippets.

@nramirez
Created January 18, 2015 22:03
Show Gist options
  • Save nramirez/ac73036716b6d0c382ab to your computer and use it in GitHub Desktop.
Save nramirez/ac73036716b6d0c382ab to your computer and use it in GitHub Desktop.
Remove Zeros
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