freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/largest-int-from-concatenat...

2.6 KiB

id title challengeType
5a23c84252665b21eecc7edb Largest int from concatenated ints 5

Description

Given a set of positive integers, write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer.

Instructions

Tests

tests:
  - text: <code>maxCombine</code> should be a function.
    testString: assert(typeof maxCombine == 'function', '<code>maxCombine</code> should be a function.');
  - text: <code>maxCombine([1, 3, 3, 4, 55])</code> should return a number.
    testString: assert(typeof maxCombine([1, 3, 3, 4, 55]) == 'number', '<code>maxCombine([1, 3, 3, 4, 55])</code> should return a number.');
  - text: <code>maxCombine([1, 3, 3, 4, 55])</code> should return <code>554331</code>.
    testString: assert.equal(maxCombine([1, 3, 3, 4, 55]), 554331, '<code>maxCombine([1, 3, 3, 4, 55])</code> should return <code>554331</code>.');
  - text: <code>maxCombine([71, 45, 23, 4, 5])</code> should return <code>71545423</code>.
    testString: assert.equal(maxCombine([71, 45, 23, 4, 5]), 71545423, '<code>maxCombine([71, 45, 23, 4, 5])</code> should return <code>71545423</code>.');
  - text: <code>maxCombine([14, 43, 53, 114, 55])</code> should return <code>55534314114</code>.
    testString: assert.equal(maxCombine([14, 43, 53, 114, 55]), 55534314114, '<code>maxCombine([14, 43, 53, 114, 55])</code> should return <code>55534314114</code>.');
  - text: <code>maxCombine([1, 34, 3, 98, 9, 76, 45, 4])</code> should return <code>998764543431</code>.
    testString: assert.equal(maxCombine([1, 34, 3, 98, 9, 76, 45, 4]), 998764543431, '<code>maxCombine([1, 34, 3, 98, 9, 76, 45, 4])</code> should return <code>998764543431</code>.');
  - text: <code>maxCombine([54, 546, 548, 60])</code> should return <code>6054854654</code>.
    testString: assert.equal(maxCombine([54, 546, 548, 60]), 6054854654, '<code>maxCombine([54, 546, 548, 60])</code> should return <code>6054854654</code>.');

Challenge Seed

function maxCombine(xs) {
  // Good luck!
}

Solution

function maxCombine (xs) {
   return parseInt(
       xs.sort(
           function (x, y) {
               var a = x.toString(),
                   b = y.toString(),
                   ab = parseInt(a + b),
                   ba = parseInt(b + a);

               return ab > ba ? -1 : (ab < ba ? 1 : 0);
           }
       )
       .join(''), 10
   );
}