Skip to content

Instantly share code, notes, and snippets.

@sodastsai
Created April 17, 2020 17:37
Show Gist options
  • Save sodastsai/ef38131b9b5d7c5ab45b3c3e87aed729 to your computer and use it in GitHub Desktop.
Save sodastsai/ef38131b9b5d7c5ab45b3c3e87aed729 to your computer and use it in GitHub Desktop.
function moveAllZerosToLeft(input) {
let zeros = [];
let numbers = [];
for (const element of input) {
if (element === 0) {
zeros.push(element);
} else {
numbers.push(element);
}
}
return zeros.concat(numbers);
}
function moveAllZerosToLeft(input) {
let result = [];
for (const element of input) {
if (element === 0) {
result.push(element);
}
}
for (const element of input) {
if (element !== 0) {
result.push(element);
}
}
return result;
}
function moveAllZerosToLeft(input) {
for (let idx = 0; idx < input.length; ++idx) {
if (input[idx] !== 0) {
continue;
}
input.unshift(input.splice(idx, 1)[0]);
}
return input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment