2019-03-21 11:52:35 +05:30
---
id: 5a23c84252665b21eecc7edb
title: Largest int from concatenated ints
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302298
2019-03-21 11:52:35 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'description' >
2019-07-18 17:32:12 +02:00
2019-03-21 11:52:35 +05:30
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.
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< / section >
## Instructions
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'instructions' >
< / section >
## Tests
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'tests' >
2020-03-30 11:23:18 -05:00
```yml
2019-03-21 11:52:35 +05:30
tests:
- text: < code > maxCombine</ code > should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof maxCombine == 'function');
2019-03-21 11:52:35 +05:30
- text: < code > maxCombine([1, 3, 3, 4, 55])</ code > should return a number.
2020-03-30 11:23:18 -05:00
testString: assert(typeof maxCombine([1, 3, 3, 4, 55]) == 'number');
2019-03-21 11:52:35 +05:30
- text: < code > maxCombine([1, 3, 3, 4, 55])</ code > should return < code > 554331</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(maxCombine([1, 3, 3, 4, 55]), 554331);
2019-03-21 11:52:35 +05:30
- text: < code > maxCombine([71, 45, 23, 4, 5])</ code > should return < code > 71545423</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(maxCombine([71, 45, 23, 4, 5]), 71545423);
2019-03-21 11:52:35 +05:30
- text: < code > maxCombine([14, 43, 53, 114, 55])</ code > should return < code > 55534314114</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(maxCombine([14, 43, 53, 114, 55]), 55534314114);
2019-03-21 11:52:35 +05:30
- text: < code > maxCombine([1, 34, 3, 98, 9, 76, 45, 4])</ code > should return < code > 998764543431</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(maxCombine([1, 34, 3, 98, 9, 76, 45, 4]), 998764543431);
2019-03-21 11:52:35 +05:30
- text: < code > maxCombine([54, 546, 548, 60])</ code > should return < code > 6054854654</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(maxCombine([54, 546, 548, 60]), 6054854654);
2019-03-21 11:52:35 +05:30
```
< / section >
## Challenge Seed
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'challengeSeed' >
2019-07-18 17:32:12 +02:00
2019-03-21 11:52:35 +05:30
< div id = 'js-seed' >
```js
function maxCombine(xs) {
// Good luck!
}
```
< / div >
< / section >
## Solution
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'solution' >
```js
2020-03-30 11:23:18 -05:00
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
);
2019-03-21 11:52:35 +05:30
}
```
2019-07-18 17:32:12 +02:00
< / section >