Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created February 17, 2020 05:13
Show Gist options
  • Save RP-3/136a6e3118953b7ed0e479843bcd52b9 to your computer and use it in GitHub Desktop.
Save RP-3/136a6e3118953b7ed0e479843bcd52b9 to your computer and use it in GitHub Desktop.
/**
* @param {number[]} nums
* @return {string}
*/
var largestNumber = function(nums) {
const result = nums
.map((num) => num.toString())
.sort((a, b) => {
const aBeforeB = parseInt(`${a}${b}`, 10);
const bBeforeA = parseInt(`${b}${a}`, 10);
return aBeforeB > bBeforeA ? -1 : 1;
})
.join('');
return containsOnlyZeroes(result) ? '0' : result.replace(/^0+/, '');
};
function containsOnlyZeroes(str){
for(let i=0; i<str.length; i++) if(str[i] !== '0') return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment