Created
July 30, 2019 05:02
-
-
Save wataruoguchi/1c15adf64153a6cd8666066f299a76bb 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
// https://practice.geeksforgeeks.org/problems/largest-number-formed-from-an-array/0 | |
function generateLargestNumber(arr) { | |
const resStr = arr.sort((a,b) => { | |
const ab = `${a}${b}`; | |
const ba = `${b}${a}`; | |
return ab === ba ? 0 : ab > ba ? -1 : 1; | |
}).reduce((acc, num) => { | |
return `${acc}${num}`; | |
}, ''); | |
return parseInt(resStr, 10); | |
} | |
const nums1 = [3,30,34,5,9]; | |
const result1 = generateLargestNumber(nums1); | |
console.log(result1, result1 === 9534330); | |
const nums2 = [54,546,548,60]; | |
const result2 = generateLargestNumber(nums2); | |
console.log(result2, result2 === 6054854654); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment