# The max() is not enough

## Intro

A friend posed this question today in a slightly different way - "Can a single [Python] command determine the largest of some integers AND that they aren't equal?".

While we didn't find a way to do this within reasonable definitions of "a single command", I thought it might be a fun problem to golf.

## Challenge

"Return the largest of a list of integers if-and-only-if they are not all equal."

More specifically:

Given a string containing only a comma-separated list of integers:

• If they are all equal, return/output nothing
• Else, return/output the largest

## Rules

• The input must be a string containing only a comma-separated list of integers
• The output must be either nothing (no output of any kind), or else the largest element from the input, represented as it is in the input

Entries may be a full program or just a function, provided you provide some way to test them!

## Assumptions

• Assume input list elements may be more than one digit but no larger than ( 232 − 1 )
• Assume the input list has no more than a million elements
• Assume the input will not include negative values
• Assume the input will never be empty

For the avoidance of doubt, the explanation of the challenge given just after "More specifically" shall supersede the statement of the challenge above it ("Return the largest...").

## Examples

(1) All equal:

Input: 1,1
Output:


(2) Dissimilar:

Input: 1,2
Output: 2


(3) Zero!:

Input: 0,0,0,0,0,0,0,1,0,0
Output: 1


(4) Random:

Input: 7,3,8,4,8,3,9,4,6,1,3,7,5
Output: 9


(5) Larger numbers, larger list:

Input: 627,3894,863,5195,7789,5269,8887,3262,1448,3192
Output: 8887


(6) All equal, larger list:

Input: 7,7,7,7,7,7,7,7,7
Output:


(7) All equal, larger list, larger numbers:

Input: 61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976,61976
Output:


(8) Not equal, larger list, larger numbers:

Input: 96185,482754,96185,96185,96185,96185,96185,96185,7,96185,96185,96185,96185,96185,96185,96185,96185,96185,96185,96185,96185,96185,96185,96185,96185,961185,96185,96185,96185
Output: 961185


## Scoring

This is code-golf, so the code with the shortest number of bytes wins!

• Is it OK to throw an error when the list contains all equal values? And can we output a null value (such as None in python) rather than outputting nothing? Also, why must the input be a string rather than a list? And what do you mean by the largest element from the input, represented as it is in the input Oct 5, 2018 at 21:02
• I'd suggest loosening the input requirements to allow for an array/list of integers, too. Are we guaranteed that the list will contain at least 2 elements? Can we output a consistent, non-numeric value instead of nothing if all integers are equal? Oct 5, 2018 at 22:08
• Please add a test case where the maximum occurs more than once, like 7,3,7,2.
– nimi
Oct 6, 2018 at 6:31
• Can we have a test case with only one element? Also, can we include a trailing , in the input?
– Jo King
Oct 6, 2018 at 7:03
• If there is exactly one element in the list, that means all elements are equal and the program should output nothing, correct? It would be good to add that as a test case. Oct 15, 2018 at 22:25

# R, 50 37 bytes

-33 bytes thanks to digEmAll! -13 bytes thanks to rturnbull!

x=scan(se=",");if(any(diff(x)))max(x)


Try it online!

• Input does (currently) HAVE to be a string, unfortunately. Oct 6, 2018 at 0:23
• 50 bytes taking a comma separated string Oct 6, 2018 at 8:42
• Oops I guess my prior link was wrong! This should be right, combining digEmAll's idea with the one I intended on posting... Oct 8, 2018 at 3:24
• 37 bytes by improving how we test the array for equality. Oct 8, 2018 at 6:09

# MathGolf, 5 bytes

è▀s╞╙


Try it online!

## Explanation

è      Read whole input as int array
▀     Get unique elements
s    Sort list
╞   Discard from left of array
╙  Get maximum of list


This works because both the max operator and the discard from left operator don't do anything for empty lists. Well, the max operator removes the list and pushes nothing for empty lists.

It could be 4 bytes if input could be taken as a list.

• Input must be a comma separated string. Oct 6, 2018 at 11:11
• @Shaggy I missed that. Right now MathGolf doesn't have a "split on character" operator, so it'll take some work to get it right. I'll see what I can do.
– maxb
Oct 6, 2018 at 11:46

# Perl 6, 26 23 22 bytes

-1 byte thanks to nwellnhof

{.max if .Set>1}o&EVAL


Try it online!

Returns an empty slip if everything is equal.

### Explanation

                o&EVAL  # Eval the string to a list of integers
{              }         # Pass to code block
.max            # Return the max
if .Set>1  # If the list converted to a set has more than one element

• The EVAL trick is neat and you can even save a byte with o&EVAL. Oct 6, 2018 at 12:25
• Wouldn’t if +.Set work?
– Ven
Mar 3, 2019 at 19:16
• @Ven No, we need to check if there is more than one element in the Set.
– Jo King
Mar 3, 2019 at 20:18
• Mh, and bitwise ~ is 2 bytes in Perl 6 :(
– Ven
Mar 3, 2019 at 21:43

# Jelly, 4 bytes

ḟṀE?


A full program accepting the input as a command line argument (unquoted) which prints the required output

(Note that it deals with: empty input like , single item input like 7 and multiple item input like 7,8,7 like the spec seems to currently require.)

Try it online!

### How?

ḟṀE? - Full program: if one argument is present it is evaluated using Python
-                 so 7,8,7 -> [7,8,7], while 7 -> 7
ḟṀE? - Main Link: list or integer OR no argument (in which case an implicit argument of 0)
? - if...
E  - ...condition: all equal? (for any integer E yields 1 since the argument is
-                           treated as a list like [integer])
ḟ    - ...then: filter discard (since it's undefined the right argument is implicitly
-                          equal to the left; both are treated as lists, so this
-                          yields an empty list)
Ṁ   - ...else: maximum (again an integer is treated as a list)
- implicit print (Jelly's representation of an empty list is an empty string
-                 furthermore no newline is printed in either case)


# APL (Dyalog Unicode), 12 bytes

Full program. Prompts for string from stdin.

{1≠≢∪⍵:⌈/⍵}⎕


Try it online!

⎕ prompt for and evaluate expression (commas concatenate the numbers into a list)

{} apply the following anonymous lambda (⍵ is the argument; the list of numbers):

1≠ [if] 1 is different from…

≢ the tally of…

∪ the unique numbers in…

⍵ the list

: then

⌈/ return the max across (lit. max reduction)…

⍵ the list

[else: do nothing]

• 1≠≢∪ here it seems find only if its argument is a list not a repetition of the same number so it is not 1 1 1 or 22 . So 1≠≢∪1 1 2 3 3 return true even if 3 is not unique and if that is true would return ⌈/⍵ the max 3 (even if it should return no output). Where is my error? Or possible ':' has a different meaning
– user58988
Oct 10, 2018 at 9:29
• @RosLuP Even if there are repetitions, and even repetitions of the max number, we still have to print the max. Only if the list has exactly one unique number do we print nothing. Look at the last example case in the OP.
Oct 10, 2018 at 9:33
• Yes I wrongly understood the problem... thanks
– user58988
Oct 10, 2018 at 9:38
• would this work? ⌈/~⌊/
– ngn
Oct 10, 2018 at 21:27
• @ngn No, it outputs a newline if all elements are equal.
Oct 10, 2018 at 21:43

# APL (Dyalog Classic), 6 bytes

⍪⌈/~⌊/


Try it online!

a train computing the maximum (⌈/) without (~) the minium (⌊/) turned into a matrix (⍪)

if the input contains only one distinct element, ⌈/~⌊/ will be empty and ⍪ will return a 0×1 matrix which renders as nothing

otherwise, ⌈/~⌊/ will be a 1-element vector and its ⍪ will be a 1x1 matrix (visually indistinguishable from a scalar) that contains the maximum

max($s)[$s!=$s]  (15 bytes - which might well be the shortest solution in a language that isn't purpose-designed for brevity) NOTE: this doesn't satisfy the requirement "represented as it is in the input" - if there's an integer with leading zeroes or a plus sign in the input, these will be lost. I suspect that's true of many other solutions as well. # K4, 38 35 bytes {$[1=#:t:?:(7h$","\:x)-48;;*:t@>t]}  Test Cases: q)k){$[1=#:t:?:(7h$","\:x)-48;;*:t@>t]}"1,2,4,4" ,4 q)k){$[1=#:t:?:(7h$","\:x)-48;;*:t@>t]}"4,4,4,4" q) q)k){$[1=#:t:?:(7h$","\:x)-48;;*:t@>t]}"7,3,8,4,8,3,9,4,6,1,3,7,5" ,9 q)k){$[1=#:t:?:(7h$","\:x)-48;;*:t@>t]}"7,7,7,7,7,7,7,7,7,7,7,7,7" q)  I'm not very fluent in any of the k variants available on TiO, so no online example available, I'll try to come up with one though Explanation If you're wondering why certain operations are performed before others, K4 does not have operator precedence, it instead interprets from right to left (though you can use parentheses for precedence). Expressions seperated by semicolons. $[expr;True;False] is the conditional format

{$[1=#:t:?:(7h$","\:x)-48;;*:t@>t]}
","\:x                 //split string on commas
• @JoKing - You shouldn't be taking input via a pre-declared variable - fixed using $argn it takes it from stdin (this has been used in multiple golf answers) I can give examples of not only mine but other users. Oct 7, 2018 at 4:57 # Japt-hF, 8 bytes q, ün Åc  Try it -3 bytes if the input could be taken as an array. # 05AB1E, 9 8 bytes ',¡ZsËiõ  -1 byte thanks to @Cowabunghole. Explanation: ',¡ '# Split the (implicit) input by "," Z # Push the maximum (without popping the list) s # Swap so the list is at the top of the stack again Ëi # If all elements are equal: õ # Push an empty string "" # (Implicitly output the top of the stack to STDOUT as result)  • Pretty similar solution, but you can avoid the else by doing ',¡ZsËiõ, saving 1 byte Oct 12, 2018 at 14:24 • @Cowabunghole Smart, thanks! Oct 12, 2018 at 17:04 # PowerShell, 45 49 bytes +4 bytes thanks Veskah ($l=$args-split','|sort{+$_}-u)[-1]|?{$l.count-1}  Try it online! Less golfed: $list=$args-split','|sort{+$_}-unuqie
$max=$list[-1]
$max|?{$list.count-1}


# Vyxal, 5 bytes

≈[¤|G


Try it Online!

≈[    # If all same
¤   # ""
|G # Else max


# Jelly, 3 bytes

ṀḟṂ


Try it online!

TIO link uses function I/O for the sake of having a test harness, but it works just fine with a plain comma-separated list. I'd prefer to let a challenge like this stay dead, but if it's been bumped anyways...

Ṁ      The maximum,
ḟ     unless it's equal to
Ṃ    the minimum.


# Python 2, 44 bytes

k=eval(input())
if~-len(set(k)):print max(k)


Try it online!

• I'm guessing eval in Python converts a comma-delimited string to a list? Oct 6, 2018 at 0:28
• Yes; more exactly a tuple (immutable list). If the input were not a string (i.e., omit quotes in TIO input section), then one could just use k=input() and get the same result. Oct 6, 2018 at 0:30

# Ohm v2, 9 bytes

Ul1E?Oq¿↑


Try it online! Explanation:

Ul1E?Oq¿↑
U         Uniquify input
l        Get length
1E      Push whether length is equak to 1
?Oq   If so immediately quit
¿↑ Else print maximum

• The input should be a string of comma separated numbers
– Jo King
Oct 5, 2018 at 22:50

# Charcoal, 15 bytes

≔Ｉ⪪Ｓ,θ¿›⌈θ⌊θＩ⌈θ


Try it online! Link is to verbose version of code. Explanation:

≔Ｉ⪪Ｓ,θ


Split the input on commas and cast each value to integer.

¿›⌈θ⌊θ


Test whether the maximum value is greater than the minimum value.

Ｉ⌈θ


If so then cast the maximum value to string and print.

# Mathematica, 43 bytes

If[!Equal@@#,Max@#]&@@#~ImportString~"CSV"&


Pure function. Takes a comma-separated string as input and returns either a number or Null. I believe this is valid, as Null is not graphically displayed: # C (gcc), 91 bytes

M(s)char*s;{long m=atol(s),o,l=0;for(;s=strchr(s,44);o<0?m-=o:0)l|=o=m-atol(++s);s=l?m:-1;}


Try it online!

### Degolf

M(s)char*s;{
long m=atol(s),o,l=0; // Read the first integer from string
for(;s=strchr(s,44); // Advance pointer to next ','
o<0?m-=o:0) // End of loop: if difference <0, deduct from max, increasing it to new max.
l|=o=m-atol(++s); // Read next number, and subtract it from current max.
// Bitwise-OR the difference into the l-variable
s=l?m:-1; // End of function: if l is non-zero, there were at least two different values.
// Return -1 if l is zero, otherwise the max value.
}

• Suggest M(char*s) instead of M(s)char*s; and index() instead of strchr() Oct 7, 2018 at 17:16

# Pyth, 7 bytes

Itl{QeS


As Pyth is based on Python, user input is always interpreted as a string, which then may be passed through eval(). All Pyth programs automatically run Q=eval(input()) as their first instruction.

Explanation:
Itl{QeS  | Full code
Itl{QeSQ | with implicit variables filled
---------+-------------------------------
I        | If
t       | one less than
l      | the length of
{Q    | the deduplicated input
| is truthy (!=0),
| print
e   | the last element of
SQ | the sorted input
`