# 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 Commented 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? Commented Oct 5, 2018 at 22:08
• Please add a test case where the maximum occurs more than once, like 7,3,7,2.
– nimi
Commented 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
Commented 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. Commented 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. Commented Oct 6, 2018 at 0:23
• 50 bytes taking a comma separated string Commented 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... Commented Oct 8, 2018 at 3:24
• 37 bytes by improving how we test the array for equality. Commented 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. Commented 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
Commented 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. Commented Oct 6, 2018 at 12:25
• Wouldn’t if +.Set work?
– Ven
Commented Mar 3, 2019 at 19:16
• @Ven No, we need to check if there is more than one element in the Set.
– Jo King
Commented Mar 3, 2019 at 20:18
• Mh, and bitwise ~ is 2 bytes in Perl 6 :(
– Ven
Commented 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
Commented 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.
Commented Oct 10, 2018 at 9:33
• Yes I wrongly understood the problem... thanks
– user58988
Commented Oct 10, 2018 at 9:38
• would this work? ⌈/~⌊/
– ngn
Commented Oct 10, 2018 at 21:27
• @ngn No, it outputs a newline if all elements are equal.
Commented 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

# CJam, 16 13 bytes

q',/:iL|$1>W>  Try it online! # Python 2, 37 bytes outputs to stderr (debug on tio). a=input();m=max(a);m>min(a)>exit(m)  Try it online! # Python 2, 42 41 bytes a=input();print('',max(a))[len(set(a))>1]  Try it online! • Welcome to PPCG :) I don't know Python but it looks like this requires input as list. Unfortunately (and unnecessarily, my opinion) the spec is (currently) very explicit that input must be a string. Commented Oct 6, 2018 at 0:25 • @Shaggy, this program works if the input is of the form element, element, .... i.e, it doesn't have to be enclosed in brackets. Commented Oct 6, 2018 at 1:27 • A list does work, but is not required because in Python 2, input by default evals whatever string is passed from stdin. Commented Oct 6, 2018 at 1:33 • 40 bytes. I don't know if a one element list can have a trailing , though, since your solution errors if the input is just a single number – Jo King Commented Oct 6, 2018 at 6:58 • @JoKing Is there a reason you could not change the != to a > as the input will never be empty? Commented Oct 6, 2018 at 18:28 # Haskell, 7775 61 bytes f.read.('[':).(++"]") f a=[0|any(/=a!!0+0)a]>>show(maximum a)  Try it online! ('[':).(++"]") takes a string (e.g. "1,2,1,3") and encloses it in bracket chars ("[1,2,1,3]"). Then read turns the string into a list of integers ([1,2,1,3]). The function f uses this tip for a shorter conditional if one of the outcomes is the empty list. any(/=a!!0+0)a checks whether the list a contains any element that is not equal to its first element a!!0. (The +0 is needed such that read knows it has to look for a list of numbers.) If all elements are equal this test results in False and the empty string is returned. Otherwise show(maximum a), that is the maximum of the list converted to a string, is returned. # Red, 81 bytes x: split input","forall x[x/1: load x/1]sort x: unique x if 1 <>length? x[last x]  Like the R solution, a huge chunk of the code is handling the input string "1,1,2,44,1". If we can have that as a block, eg: x: [1 1 2 44 1], then we can do it in 41 bytes: sort x: unique x if 1 <>length? x[last x]  • Welcome to PPCG! We typically ask for a link to an interpreter to verify a solution, and Dennis, one of our mods, has set up Try it online! for just such a purpose! It will even format your answer for you! Hope you enjoy your time spent here! Commented Oct 6, 2018 at 0:40 • sort x: unique load replace/all input","" "if 1 <>length? x[last x] for 67 bytes. Unfortunately input doesn't work in TIO. If you make it a func, it works just fine in TIO: 73 bytes Commented Oct 6, 2018 at 6:49 # JavaScript (Node.js), 49/53 bytes ## My original version using .every(), 53 bytes Does a function returning '' count as no output? Sure this can be improved upon... s=>(a=s.split,).every(e=>a[0]==e)?'':Math.max(...a)  Try it online! ## Improved version using Set() by Shaggy, 49 bytes s=>new Set(a=s.split,).size>1?Math.max(...a):  Try it online! • A very quick improvement: tio.run/##y0osSyxOLsosKNHNy09J/Z9m@7/… Commented Oct 5, 2018 at 22:18 • @Shaggy nice! I started off using Set but couldn't get it as short as that Commented Oct 5, 2018 at 22:21 # Neim, 4 bytes 𝐐𝐎Ξ𝐠  Explanation:  Ξ If 𝐐 all elements are equal 𝐎 not then 𝐠 get greatest element  Try it online! • Currently, the input must be a comma-delimited string of integers but I've asked if we can take an array instead. Commented Oct 5, 2018 at 22:10 # Octave, 28 bytes Returns the maximum (a number, which is an 1x1 matrix) or an empty (1x0) matrix. @(a)max(a)(1+all(a(1)==a):1)  Try it online! • Input is a string of comma separated numbers – Jo King Commented Oct 5, 2018 at 22:49 # Japt, 16 bytes This would be 9 if not for the unnecessarily strict input format, 7 if throwing an error counts as outputting nothing. Assumes the string contains at least 2 integers. q, mn â ÊÉ?Urw:P  Try it # Common Lisp, 102 bytes (lambda(x &aux(c(read-from-string(concatenate'string"#.("x")"))))(or(apply'= c)(princ(apply'max c))))  Try it online! The size is mainly due to inputting the data; with input as a regular list, the length reduces to 46 bytes: (lambda(x)(or(apply'= x)(princ(apply'max x))))  # 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. Commented 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 Commented Oct 12, 2018 at 14:24 • @Cowabunghole Smart, thanks! Commented 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? Commented 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. Commented 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
Commented 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() Commented Oct 7, 2018 at 17:16

# XPath 3.1, 54 bytes

with the input string as the context item:

let$t:=tokenize(.,',')!xs:int(.)return max($t)[$t!=$t]


Could be reduced by one character if you allow the context to bind a shorter prefix than "xs" to the XML Schema namespace.

Explanation: takes the input string, tokenizes on "," separator, applies xs:int() to each token to convert to an integer, computes the max of the sequence, outputs the max provided the predicate $t!=$t is true. If A and B are sequences, then A!=B is true iff there is a pair of items (a from A, b from B) such that a!=b.

If the input can be supplied as a sequence of integers $s rather than a comma-separated string then the solution reduces to 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.

# 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
`