Skip to content

Instantly share code, notes, and snippets.

@Ephraimiyanda
Created February 16, 2026 15:11
Show Gist options
  • Select an option

  • Save Ephraimiyanda/d496c87b15270ce6ff8ca2bf38fbe3e3 to your computer and use it in GitHub Desktop.

Select an option

Save Ephraimiyanda/d496c87b15270ce6ff8ca2bf38fbe3e3 to your computer and use it in GitHub Desktop.
Largest Number

Question

Approach

we are comparing the concactenation of numbers not their addition. so the numbers are turned to strings and are sorted based on their concactenation in descending order and are then joined an returned.

Complexity

  • Time complexity:O(N log N)

  • Space complexity:O(N)

Code

function largestNumber(nums: number[]): string {
   const result = nums
    .map(String)
    .sort((a, b) => {
      return a.concat(b) > b.concat(a) ? -1 : 1;
    })
    .join('');

  return result.charAt(0) === '0' ? '0' : result;
};
scrnli_nZ15yppU8AxZc4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment