47
\$\begingroup\$

Given a nonempty list of positive decimal integers, output the largest number from the set of numbers with the fewest digits.

The input list will not be in any particular order and may contain repeated values.

Examples:

[1] -> 1
[9] -> 9
[1729] -> 1729
[1, 1] -> 1
[34, 3] -> 3
[38, 39] -> 39
[409, 12, 13] -> 13
[11, 11, 11, 1] -> 1
[11, 11, 11, 11] -> 11
[78, 99, 620, 1] -> 1
[78, 99, 620, 10] -> 99
[78, 99, 620, 100] -> 99
[1, 5, 9, 12, 63, 102] -> 9
[3451, 29820, 2983, 1223, 1337] -> 3451
[738, 2383, 281, 938, 212, 1010] -> 938

The shortest code in bytes wins.

asked Sep 16, 2016 at 0:27
\$\endgroup\$
2
  • \$\begingroup\$ Can the input numbers be on separate lines? \$\endgroup\$ Commented Sep 16, 2016 at 5:48
  • \$\begingroup\$ @seshoumara That sounds reasonable, yes. \$\endgroup\$ Commented Sep 16, 2016 at 5:48

68 Answers 68

1 2
3
0
\$\begingroup\$

Java 8, 110 bytes

int f(int[]a){java.util.Arrays.sort(a);int i=a[0];for(int z:a)i=(z+"").length()>(i+"").length()?i:z;return i;}

Ungolfed:

int f(int[]a){
 java.util.Arrays.sort(a); //sorts the array in ascending order
 int i=a[0]; //declare output variable, uses 1 less byte than using a[0]
 for(int z:a) {
 i=(z+"").length()>(i+"").length()?i:z; //for each int, if the length isn't longer set as output
 }
 return i;
}

Any elements that aren't longer in a sorted array must be a higher value.

Also lambda version (101 bytes):

a->{java.util.Arrays.sort(a);int i=a[0];for(int z:a)i=(z+"").length()>(i+"").length()?i:z;return i;};
answered Sep 16, 2016 at 13:50
\$\endgroup\$
0
\$\begingroup\$

Python 2, (削除) 63 (削除ここまで) (削除) 30 (削除ここまで) 63 bytes

lambda a:a.sort()or[`i`for i in a if len(`i`)==len(`a[0]`)][-1]
answered Sep 16, 2016 at 0:45
\$\endgroup\$
3
  • \$\begingroup\$ Bugged for [1, 99, 620, 10000, 3]. \$\endgroup\$ Commented Sep 16, 2016 at 1:54
  • \$\begingroup\$ I agree with orlp, doesn't work \$\endgroup\$ Commented Sep 16, 2016 at 2:07
  • \$\begingroup\$ Once again, a wrong anser with an upvote? This should be amended or deleted \$\endgroup\$ Commented Sep 16, 2016 at 13:27
0
\$\begingroup\$

J-uby, 22 bytes

Port of my Ruby solution.

:max_by+-[S|:+@|:-@,I]

Attempt This Online!

answered Sep 10, 2024 at 21:16
\$\endgroup\$
0
\$\begingroup\$

Tcl, 93 bytes

proc D L {proc S x\ y {expr [string le $y]<=[string le $x]}
lindex [lsort -c S [lsort $L]] 0}

Try it online!

answered Nov 1 at 1:28
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 38 + 3 (-pla flag) = 41 bytes

($_)=sort{$a=~y///c-length$b||$b-$a}@F

Try it online!

answered Nov 1 at 13:27
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 34 + 4 (-plF ) = 38 bytes

$a[$#F]=$_ if$_>$a[$#F]}{$\=1*"@a"

Try it online!

Linear time based on the number of entries in the list. Memory space is proportional to the length of the longest number.

answered Nov 3 at 5:04
\$\endgroup\$
0
\$\begingroup\$

AWK, 54 bytes

n||n=99{(n>x=length)&&n=x;x-n||0ドル>r&&r=0ドル}END{print r}

Attempt This Online!

Takes input numbers on separate lines.

answered Nov 4 at 19:33
\$\endgroup\$
0
\$\begingroup\$

TI-84 BASIC (TI-84 Plus CE Python), 17 bytes

Input A
int(log(⸤A
max(⸤A*(Ans=min(Ans

gets the logarithm of each number in the list (int(log(⸤A), removes numbers that don't have the smallest amount of digits (⸤A*(Ans=min(Ans), and returns the max

answered Nov 5 at 15:32
\$\endgroup\$
1 2
3

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.