Created
April 17, 2020 17:37
-
-
Save sodastsai/ef38131b9b5d7c5ab45b3c3e87aed729 to your computer and use it in GitHub Desktop.
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 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); | |
} |
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 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; | |
} |
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 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