# Compare Two Lists by Their Maximum

Given two different lists of non-negative integers, return the list that has the highest maximum (e.g. [4, 2], [3, 3, 1] -> [4, 2]).

If they both have the same maximum, return the list that contains more instances of this maximum (e.g. [4, 2, 4], [4, 3, 3, 3, 1, 3] -> [4, 2, 4]).

If, after these comparisons, they are equal, do the same comparison but with their next highest item (e.g. [2, 3, 4, 4], [4, 4, 3, 3, 1] -> [4, 4, 3, 3, 1]).

If, after all these comparisons, they are still considered to be equal, output the longer list (e.g. [4, 3, 2, 1, 0], [1, 2, 3, 4] -> [4, 3, 2, 1, 0]).

Make your code as short as possible.

### Test Cases

[4, 4, 4, 4, 2, 4], [4, 4, 4, 4, 3, 2] -> [4, 4, 4, 4, 2, 4]
[0], [] -> [0]
[0, 0], [0] -> [0, 0]
[1], [0, 0] -> [1]
[4, 4, 4, 4, 4, 2], [4, 4, 4, 4, 4] -> [4, 4, 4, 4, 4, 2]
[1, 0], [0, 0, 0] -> [1, 0]


# Pyth, 4 bytes

eo_S


Try it online

### Explanation

eo_S
o  NQ    Order the inputs...
_S      ... by their reversed sorted values...
e         ... and take the last.


# Python 2, 44 bytes

lambda*x:max(x,key=lambda y:sorted(y)[::-1])


Try it online!

• Is this valid for 40 bytes? Commented Jan 23, 2018 at 20:33
• No, unfortunately. Commented Jan 23, 2018 at 20:37

# Jelly, 4 bytes

NÞÞṪ


Try it online!

### How it works

NÞÞṪ  Main link. Argument: [u, v] (pair of vectors)

Þ   Sort [u, v], using the link to the left as key.
NÞ      Sort u (or v) by the negatives of its values.
This sorts the vector in descending order.
Ṫ  Tail; select the last, lexicographically larger vector.


import Data.Lists
argmax$sortOn(0-)  The input is taken as a two element list, e.g. ( argmax$sortOn(0-) ) [[4,4,4,4,2,4], [4,4,4,4,3,2]].

Find the element in the input list which is the maximum after sorting by negating the values (i.e. descending order).

# Clean, 42 bytes

import StdEnv
s=sortBy(>)
?a b|s a>s b=a=b


Try it online!

• Huh, that's an interesting syntax. I take it it means "return a if the pattern guard is true and b otherwise"? Commented Jan 22, 2018 at 21:02
• @Laikoni Yep. Pattern guards in Clean are syntactically similar to a C-style if ... else if ..., where you can nest/chain them. And just like you can omit else if(true), you can skip the last guard condition. (however, only once per line) Commented Jan 22, 2018 at 21:09

# JavaScript (ES7), 52 bytes

(a,b,m=d=>d.map(n=>N+=1e4**n,N=0)&&N)=>m(a)>m(b)?a:b


This method works without sorting the arrays. Instead, it calculates the sum of 10,000 raised to each array's elements. The largest sum represents the array with the highest score. (This solution assumes that neither array has more than 10,000 elements.)

Test cases

let f=

(a,b,m=d=>d.map(n=>N+=1e4**n,N=0)&&N)=>m(a)>m(b)?a:b

console.log(JSON.stringify(f([4, 4, 4, 4, 2, 4], [4, 4, 4, 4, 3, 2]))); // [4, 4, 4, 4, 2, 4]
console.log(JSON.stringify(f([0], []))); // [0]
console.log(JSON.stringify(f([0, 0], [0]))); // [0, 0]
console.log(JSON.stringify(f([1], [0, 0]))); // [1]
console.log(JSON.stringify(f([4, 4, 4, 4, 4, 2], [4, 4, 4, 4, 4]))); // [4, 4, 4, 4, 4, 2]
console.log(JSON.stringify(f([1, 0], [0, 0, 0]))); // [1, 0]
console.log(JSON.stringify(f([3], [11]))); // [11]
console.log(JSON.stringify(f([4, 3, 3, 3, 1, 3], [4, 2, 4]))); // [4, 2, 4]
console.log(JSON.stringify(f([3,3],[3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]))); //[3, 3]

# Ruby, 33 bytes

->*a{a.max_by{|x|x.sort.reverse}}


Try it online!

# Python 2, 79 bytes

lambda*a:max(a,key=lambda l:sum(zip(map(l.count,s(l)),s(l)),())[::-1])
s=sorted


Try it online!

# Ruby 149 bytes

->n,m{(n+m).sort.reverse.map{|i|r=((a=n.index(i))&&m.index(i)?(b=n.count(i))==(c=m.count(i))?nil:b>c ?n:m:a ?n:m);return r if r};n.size>m.size ? n:m}


Try it online!

# Husk, 3 bytes

►Ö_


Try it online!

This submission basically takes a two-element list of lists as input and retrieves the maximum (►), sorted by their values sorted in descending order (Ö_).

# 05AB1E, 5 bytes

Σ{R}θ


Try it online!

## JavaScript (ES6), 98 bytes

(a,b,g=a=>[...a].sort((a,b)=>b-a),h=([a,...b],[c,...d])=>a==c?h(b,d):a<c|!(1/a))=>h(g(a),g(b))?b:a


g sorts a copy of its parameter in reverse order (since sort mutates the array), while h recursively performs the elementwise comparison of the arrays.

# Perl 6, 17 bytes

*.max(*.sort(-*))


Test it (Lambda Lambda Lambda)

• -* lambda that numerically negates the input
• *.sort(-*) lambda that uses that uses the results of applying that to compare elements
• *.max(*.sort(-*)) lambda that finds the max of those results, and uses that to determine which input to return.

# J, 20 bytes

[:{.]\:[:#.[:>\:~&.>


Ungolfed:

[: {. ] \: [: #. [: > \:~&.>


Essentially the Pyth answer, translated unpithily into J.

Try it online!