## DEV Community is a community of 879,630 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

dev.to staff

Posted on

# Daily Challenge #177 - Supersize Me

### Setup

Implement a function that rearranges an integer into its largest possible value. If the integer is already in its maximum possible value, simply return it.

### Examples

`superSize(123456)` //654321
`superSize(105)` //510

### Tests

`superSize(513)`
`superSize(2020)`
`superSize(4194)`
`superSize(608719)`
`superSize(123456789)`
`superSize(700000000001)`
`superSize(666666)`

Good luck!

This challenge comes from ozymandias1 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

## Discussion (16)

SavagePixie

JavaScript oneliners for the win!

``````const superSize = n => +n.toString().split('').sort((a, b) => b - a).join('')
``````
George WL

Why the plus though?

Craig McIlwrath

The argument to the unary plus operator is a string. The + is a very concise method of converting a string to a number, in this situation.

George WL

Ah, I totes prefer the readability over conciseness approach

dart

``````int superSize(int number) {
var data = number.toString();
var dataInt = List<int>();
for(var n = 0; n < data.length; n++) {
var val = int.tryParse(data[n]);
if(val != null) {
}
}
dataInt.sort((a, b) => b.compareTo(a));
return int.tryParse(dataInt.join());
}
``````

I'm sure this could be done shorter.

Dimitri Marion

Javascript

``````const superSize = n => {
const nArr  = Array.from(n.toString()).map(Number);

const nArrSorted = nArr.sort((a, b) => b - a);

return parseInt(nArrSorted.join(''));
};
``````
Jehiel Martinez

In JS, implementing Bubble Sort just because.

``````function superSize(num) {
let str = [...num.toString()];
let swap;
do {
swap = false
for (i = 0; i <= str.length; i++) {
if (str[i] < str[i + 1]) {
const bigger = str[i + 1];
str[i + 1] = str[i];
str[i] = bigger;
swap = true
}
}
} while (swap);
return(+str.join(''));
};
``````
Andrei Visoiu

in C++ using the standard template library:

``````typedef unsigned long long ull;
ull superSize(ull x) {
vector<short> d;
while(x > 0) {
d.push_back(x%10);
x /= 10;
}
sort(d.begin(), d.end(), greater<short>());
ull r = 0;
for(auto i: d)
r = r*10 + i;

return r;
}
``````

Javascript solution for superSize :)

``````function superSize(number){
let revNum = "";
let tempNum = number;
while(number != 0){
revNum = `\${revNum}\${(number % 10)}`;
number = parseInt(number / 10);
}
revNum = parseInt(revNum);
return revNum > tempNum ? revNum : tempNum;
}
``````
Valts LiepiΕΕ‘

Yet another Ruby one liner:

``````def superSize int
int.to_s.chars.sort.reverse!.join.to_i
end
``````

Also neat how it reads almost literarly, no need to explain each step in the chain!

lusen / they / them π³οΈβππ₯
``````def super_size(num):
integers = [int(c) for c in str(num)]
integers.sort(reverse=True)
return ''.join([str(i) for i in integers])
``````
zoe-j-m • Edited on

Scala

``````def superSize = (_ : Int).toString.sorted.reverse.mkString
``````
Mohamed ABDELLANI • Edited on
``````def superSize(x)
x.to_s.to_s.split("").sort{|x,y|-(x<=>y)}.join.to_i
end
``````
Craig McIlwrath

``````import Data.Char (digitToInt)