Skip to content

Instantly share code, notes, and snippets.

@KevinGomezDev
Last active June 2, 2021 14:48
Show Gist options
  • Save KevinGomezDev/9a3535ad6766761070e3e83df3e90aaf to your computer and use it in GitHub Desktop.
Save KevinGomezDev/9a3535ad6766761070e3e83df3e90aaf to your computer and use it in GitHub Desktop.
//Having a list of n digits (0 <= digit <= 9), where n less than or equal to 100, arrange it to move all 0 to right in O (n) time.
//Example [4, 8, 0, 9, 2, 5, 0, 3, 3, 0] -> [4, 8, 9, 2, 5, 3, 3, 0, 0, 0]
//You must display this list in the console.
//After this, the previous list without zeros ([4, 8, 9, 2, 5, 3, 3]) will represent a fictitious integer (4892533). You should add 1 unit to it, leaving the final list as [4, 8, 9, 2, 5, 3, 4]. You must display this list in the console.
function moveCeroToRight(numbers) {
let count = 0
for (let i = 0; i < numbers.length; i++){
if(numbers[i] !== 0) {
numbers[count++] = numbers[i];
}
}
for (let i = count; i < numbers.length; i++){
numbers[i] = 0
}
return numbers
}
function plusOneInteger(numbers) {
const stringNumber = numbers.join(',').replace(/,/g, '')
const fictioniusInteger = Number(stringNumber)
const plusedNumber = fictioniusInteger + 1
const newPlussedArray = Array.from(String(plusedNumber), Number)
return newPlussedArray
}
console.log('List1', moveCeroToRight([4, 8, 0, 9, 2, 5, 0, 3, 3, 0]))
console.log('List2', plusOneInteger([4, 8, 9, 2, 5, 3, 3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment