# Output the largest number with the fewest digits

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.

• Can the input numbers be on separate lines? Sep 16, 2016 at 5:48
• @seshoumara That sounds reasonable, yes. Sep 16, 2016 at 5:48

# Factor, 25 bytes

[ all-shortest supremum ]


Try it online!

Get the largest of the shortests.

# Japt-h, 4 bytes

Input as an array of integer strings, output as an integer string.

üÊÎñ


Try it

üÊÎñ     :Implicit input of array
ü        :Group and sort by
Ê       :  Length
Î      :First element
ñ     :Sort
:Implicit output of last element


# Vyxal, 5 bytes

⁽₍LN∵


Try it Online!

## How?

⁽₍LN∵
⁽     # Next element as a lambda:
₍    #  Apply both of the next two elements and wrap the results in a list:
L   #   Length...
N  #   ...and negate
∵ # Get the minimum of the input by that function


# Pip, 11 bytes

@Y#_AE-_SKa


Try It Online!

• Great idea, but unfortunately it doesn't work. If you put the length and the negated value in a List instead of a Range, it works; and you can get back to 11 bytes by using Y. Jun 15 at 18:35

# C (clang), 77 bytes

m,n,i,j;f(*a,l){for(j=99;l--;m=i<j|i>j^n>m?j=i,n:m)i=log10(n=a[l]);return m;}


Try it online!

## Python 2, 58 bytes

def F(x):l={len(i):i for i in sorted(x)};print l[min(l)]


# Python 3, 56 bytes

lambda a:sorted(sorted(a),key=lambda x:-len(str(x)))[-1]


Uses a lambda in a lambda!

## Python 2, 53 bytes

s=lambda a:sorted(sorted(a),key=lambda x:-len(x))[-1]


Same but with backticks

## Pip, 11 bytes

(SNgSK-#_v)


Takes input as command-line args. Try it online!

First time using the Sort-Keyed operator! Like Python's sorted(), it takes a function that is applied to each item of the iterable and the result used as a sort key. Here's how this program works:

 SNg         List of cmdline args, sorted numerically in increasing order
SK       Sort with key function...
-#_    ... negative length(x), thus putting the shortest numbers at the end but not
affecting the relative ordering among numbers with the same length
(        v)  Get the last element (index -1) and auto-print


# Clojure, 63 bytes

(reduce #(if(=(quot %1 10)(quot %2 10))(max %1 %2) %1)(sort x))


as in:

(reduce #(if(=(quot %1 10)(quot %2 10))(max %1 %2) %1)(sort[3 7 121 11 8 2 10 9]))
=> 9


Though I'm sure there's a way to make it smaller.

# PHP , 86 Bytes

<?$l=strlen($r=min($a=$_GET[a]));foreach($a as$v)if($v>$r&strlen($v)==$l)$r=$v;echo$r;  ## Pyke, 7 bytes .#_il)h  Try it here! # C#, 119 bytes int L(int[]n){var g=n.GroupBy(o=>(o+"").Length);return g.Where(o=>o.Key==g.Min(k=>k.Key)).OrderBy(o=>o).First().Max();}  Ungolfed: public int L(int[]n) { var g = n.GroupBy(o => (o+"").Length); return g.Where(o => o.Key == g.Min(k => k.Key)).OrderBy(o => o).First().Max(); }  Magical Linq... Usage: var a = new LargestNumberFewestDigits(); Console.Write(a.L(new int[] { 738, 2383, 281, 938, 212, 1010 }));  Output: 938  ## 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;};  # C# (85 bytes) It's a full function: int y(int[] t) {int m=t.Min(i=>(i+"").Length);return t.Max(n=>(n+"").Length==m?n:0);}  Unriddled: int y(int[] t) { int m = t.Min(i => (i+"").Length); return t.Max(n => (n+"").Length == m ? n : 0); }  • There's an useless whitespace after you function signature and in the parameter list. Lambda statement should also work. Sep 17, 2016 at 17:21 # Python 2, 6330 63 bytes lambda a:a.sort()or[ifor i in a if len(i)==len(a[0])][-1]  • Bugged for [1, 99, 620, 10000, 3]. – orlp Sep 16, 2016 at 1:54 • I agree with orlp, doesn't work Sep 16, 2016 at 2:07 • Once again, a wrong anser with an upvote? This should be amended or deleted Sep 16, 2016 at 13:27 # C# 85 bytes int S(int[] i){var m=i.Min();return i.Where(v=>v<m*10&&$"{v}{m}".Length%2==0).Max();}


Usage:

Console.WriteLine(S(new[] { 738, 2383, 281, 938, 212, 1010 }));


Output: 938

Explanation

Get the minimum value from the array and assume that the minimum value is the value with the shortest length.

Filter the array to remove all values greater than 10 * the minimum. i.e if the value is 8, the first check removes all values greater than 80.

Also filter to remove all values where the length of the combined string is odd. "8" + "9" is length 2 which is even, "8" + "10" is length 3 which is odd and eliminated.

int S(int[] i)
{
var m = i.Min();
return i.Where(v => v < m * 10 && \$"{v}{m}".Length % 2 == 0).Max();
}

• int[]i will work. Sep 17, 2016 at 17:22

# C# 72 bytes

int S(int[]i){return i.OrderBy(v=>(""+v).Length).ThenBy(v=>-v).First();}


Borrowing from some of the other answers, a very efficient way to accomplish this task is to sort by number of digits, then by value descending. This moves the answer to the first position. Linq accomplishes this fairly simply with the OrderBy and ThenBy statements.

Console.WriteLine(S(new[] { 738, 2383, 281, 938, 212, 1010 }));


Output: 938

## Racket 148 bytes

(λ(l)(define l2(map number->string l))(apply max(map string->number
(filter(lambda(x)(eq?(string-length x)(apply min(map string-length l2))))l2))))


Testing:

(f (list 1 5 9 12 63 102))


Output:

9


Detailed version:

(define (f sl)
(define sl2 (map number->string sl))
(apply max
(map string->number
(filter (lambda(x)
(equal? (string-length x)
(apply min (map string-length sl2))
)) sl2))))


## SQLite, 49 bytes

SELECT n FROM a ORDER BY length(n),n DESC LIMIT 1


Of course only if the list is allowed to be in a table. The type of the field should be INTEGER.

## C++11, 109 bytes

[](auto a){sort(a.begin(),a.end(),[](auto x,auto y){return (int)log10(x)<=(int)log10(y)&&x>y;});return a[0];}


a should be a container like vector or list. E.g. (lambda as f)

cout<<f(vector<int>{ 10, 13, 19, 100, 12200 });


# Common Lisp, 136 bytes

Golfed:

(defun o(&rest r &aux(s(sort r '<)))(flet((f(x)(floor(log x 10))))(apply 'max(remove-if(lambda(x)(<(apply 'min(mapcar 'f s))(f x)))s))))


Ungolfed:

(defun o (&rest r
&aux (s (sort r '<)))
(flet ((f (x) (floor (log x 10))))
(apply 'max
(remove-if (lambda (x)
(< (apply 'min (mapcar 'f s))
(f x)))
s))))


# Object Pascal, 169 167 bytes

A function which takes variable array as input:

function m(a:array of integer):integer;var i:integer;begin m:=a[0];for i in a do if(int(log10(i))<int(log10(m)))or((int(log10(i))=int(log10(m)))and(i>m))then m:=i;end;


(Yes I know, pascal sucks at this, in addition there is no sort function in it's library..)

# Coconut, 56 bytes

This is just a Coconut-ification of the best (currently only) Python 3 answer (as well as some of the other answers), so most of the credit should go to them. Any valid Python 3 is Coconut which means it's at most the same length. Also, Coconut compiles to Python.

lambda a:sorted(sorted(a),key=lambda x:-len(str(x)))[-1] can be changed into ->sorted(sorted(_),key=->-len(str(_)))[-1]. This compiles to lambda _=None: sorted(sorted(_), key=lambda _=None: -len(str(_)))[-1] (along with a ton of other stuff like additional Coconut builtins).

# JavaScript (Node.js), 53 bytes

a=>a.reduce((N,n)=>~(L=Math.log10)(N)!=~L(n)^N>n?N:n)


Attempt This Online!

## Explanation

For any two values N,n, we need to decide which to return, this table describes that:

         length
| < = >
---+-------
value < | N n .
= | . ? .
> | . N n


Where . will never happen and ? means either N or n works.

This looks a lot like an exclusive or (XOR) table:

      A
| 0 1
---|-----
B 0 | 0 1
1 | 1 0


Where we XOR length(N) != length(n) with N > n. The cheapest way (I think) to get the length of a number in JS is using Math.log10, then flooring the result with a bitwise operator (in this case ~).

I also [ab]use the fact that ^ has lower operator precedence than comparisons.