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
• Info: This challenge was sandboxed. /// For OP: Although the sandbox is pretty inactive, (I think) it is only a minor problem. The problem is that some problems with the challenge are not apparent if one only reads the challenge, and can only be discovered when one actually solves it. Oct 6, 2018 at 11:00

{.max x![==] $_}o&EVAL  Try it online! -6 bytes thanks to @ASCII-only • Links on this one as well please :P (just save the explanation somewhere and add it back in after pasting the autogenerated submission) Mar 1, 2019 at 11:10 • I need to get into the habit. Ty – Ven Mar 1, 2019 at 11:11 • Preferably with the testcases in the question (and optionally more) :P (use header to assign the function, footer to run testcases) Mar 1, 2019 at 11:15 • Also, seems to be invalid. "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" Mar 1, 2019 at 11:17 • something like this Mar 1, 2019 at 11:19 Python 3.8 (pre-release), 45 44 bytes if~-len(a:=set(eval(input()))):print(max(a))  Try it online! First time golfing; I'm sure this can be improved. Also a few months late to the party on this challenge, but whatever. xD 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}


Gol><>, 36 bytes

TIE!tlK1}l4,MF={=}|{?;1W~2K(?$~lM|~h  4 bytes golfed off!!! I realized I was using some redundancies in there... next I think I can really make the part for checking if the array is equal smaller! Try it online! # Gol><>, 40 bytes TIE!tlK1}l4,MF={S&}|{?;1W~2K(Q$~:|~lM|~h


Haha, I had my debug stuff in my previous answer, now this has golfed off 2 bytes, but I just realized how to make it even shorter

Try it online!

Runic Enchantments, 39 bytes

/u'!}R:'!=?\~or:}(\

Bash (coreutils), 28 bytes

sort -n|uniq|tail +2|tail -1


Input is a stream, one number on each line

tail +2 strips the first line, and the output is empty if it's the only line coming out from uniq, which squashes identical lines into one after sort.

Rust, 128 bytes

fn f(s:&str)->Option<u32>{let v=s.split(",").map(|a|a.parse::<u32>().unwrap());let x=v.clone().max();if v.min()==x{None}else{x}}


Try it online!

F#, 112 bytes

let m(c:string)=
let n=c.Split ','|>Seq.map int
if Seq.distinct n|>Seq.length=1 then None else Seq.max n|>Some


Try it online!

I get the feeling that it could be made smaller, but I can't quite see how...

Ruby, 39 bytes

a=eval gets
puts a.max if a.uniq.size>1


Takes an string-form array and prints the result.

• Input should be taken as a string Oct 8, 2018 at 6:25

Java (JDK), 238 bytes

Golfed

public static String max(String s){StringTokenizer st=new StringTokenizer(s,",");TreeSet<Integer>t=newTreeSet<>();while(st.hasMoreTokens()){t.add(Integer.parseInt(st.nextToken()));}if(t.first()=t.last())return "";return Integer.toString(t.last());}


Ungolfed

import java.util.StringTokenizer;
import java.util.TreeSet;

public class max {
public static String max(String s) {
StringTokenizer st = new StringTokenizer(s,",");
TreeSet<Integer> t = new TreeSet<>();
while (st.hasMoreTokens()) {
}
if (t.first() == t.last()) return "";
return Integer.toString(t.last());
}
}


I have already seen this answer, but I still wanted to share my answer.

• Hey! Few general tips for golfing in Java (especially 8+); You can use the lambda declaration to shave off almost all of the public static String max(String s) preamble, try and avoid using if expressions and use ternary operators instead, and any imports (e.g. import java.util.*) count towards your byte count, so avoid if possible! Anyway, check out this link for a whole load of tips and welcome to CodeGolf! Oct 8, 2018 at 8:13
• @LukeStevens Thanks for the tips! I will work on fixing this answer soon! Oct 10, 2018 at 2:31

$_=$F.uniq.one?? p: $F.map(&:to_i).max  Try it online! I'm generally not a fan of extensively using command line switches, but since the I/O format is very rigid - here it goes. The input is automatically (-a) split into an array $F using comma as a separator (-F,). If it contains only one unique element, we return nil, otherwise, the max value. Finally, the output written into $_ is implicitly printed (-p). The option -l isn't really necessary - it is only there to make empty outputs discernible by line breaks. Update: Almost forgot that we don't automatically get integers from split, just strings, which brings more boilerplate... Perl 5 -n -M5.010, 54 bytes use List::Util'/.+/';@n=split/,/;uniq(@n)>1&&say max@n  Try it online! V, 22 bytes Ó,/ò ún Dç^"$/d
GYVHp


Try it online!

Hexdump:

00000000: d32c 2ff2 0afa 6e0a 44e7 5e12 2224 2f64  .,/...n.D.^."$/d 00000010: 0a47 5956 4870 .GYVHp  Explanation: Ó,/ò Replace commas with new lines ún Sort; cursor ends up in first line D Delete line (smallest number) ç^<C-r>"$/d        Delete all lines that contain the same number
GYVHp               Go to last line, copy, select all, paste
If all numbers are equal, the buffer is empty and nothing will be copied,
resulting in an empty output


PHP, 57 bytes

<?=count(array_unique($a=split(",",$argn)))>1?max($a):"";  requires PHP<7; replace split with explode for current PHP (+2 bytes): <?=count(array_unique($a=explode(",",$argn)))>1?max($a):"";


Run as pipe with -nR or try them online.

Ruby, 32 bytes

->l{eval(l).minmax.uniq[1,1]*''}


Try it online!

Scala (91 79 bytes):

def f(s:String){val i=s.split(",");if(i.toSet.size>1)print(i.map(_.toInt).max)}


Try it online

APL (Dyalog Unicode), 12 bytes

(⌈/↑⍨⍬≢1↓∪)⎕


Try it online!

If there were no input requirements, it could just be ⌈/↑⍨⍬≢1↓∪ for 9 bytes.

Bash, 36 bytes

tr , \\n|sort -un|tail -n+2|tail -n1


takes input on stdin, outputs to stdout

$echo 1,2,3 | ./f.sh 3$ echo 1,1,1 | ./f.sh
\$ echo 123,231,321,312 | ./f.sh
321


Java (JDK), 112108 117 bytes

s->{return java.util.Arrays.stream(s.split(",")).mapToLong(Long::new).distinct().sorted().skip(1).max().getAsLong();}


Try it online!

Either returns a long or throws an exception, which presumably counts as not returning anything.

Edit: Fixed a bug that caused incorrect output if the maximum was the first element of the input. (+9 bytes)