# Most contributing rows

Given a non-empty matrix of non-negative integers, answer which unique rows contribute most to the sum total of elements in the matrix.

Answer by any reasonable indication, for example a mask of the unique rows order of appearance (or sort order), or indices (zero- or one- based) of those, or a submatrix consisting of the rows (in any order) or some kind of dictionary construct… — but do explain it!

## Examples

[[1,2,3],[2,0,4],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]:

The unique rows are [1,2,3], [2,0,4], and [6,3,0] each respectively contributing 6, 6, and 9 each time they occur. However, they occur once, thrice and twice respectively, so all of their respective occurrences contribute 6, 18, and 18 to the total (42), so the latter two rows are the ones that contribute most. Valid answers are therefore:

[false,true,true] mask in appearance/sort order or
[1,2]/[2,3] zero/one-based indices of the above or
[[2,0,4],[6,3,0]] the actual rows

[[1,2],[3,1],[2,3],[1,2],[3,1],[2,3],[1,2]]

[false,false,true](appearance order) / [false,true,false](sort order)
/(appearance order) / /(sort order)
[[2,3]]

# Jelly, 5 bytes

Ġị§§M


Try it online!

1-based indices of sorted unique elements of input.

• Woha, that is short. – Adám Dec 31 '18 at 16:44

# Pyth, 9 bytes

-1 thanks to FryAmTheEggman!

{s.MssZ.g


Try it online!

• The final k isn't necessary. Also .M*sZ/QZ{ seems to be a same-length solution then. – FryAmTheEggman Dec 31 '18 at 21:12
• @FryAmTheEggman Oh lol how did I forget about auto-complete? Thanks a lot! – Mr. Xcoder Dec 31 '18 at 21:42

# R, 64 bytes

function(M)max(x<-tapply(rowSums(M),apply(M,1,toString),sum))==x


Try it online!

Returns a boolean vector with TRUE/FALSE in sort order (lexicographic).
The unique rows are shown as vector names, so it is easy to identify the most contributing ones.

# Python 3, 153145 129 bytes

-8 bytes thanks to @Mr. Xcoder!

from itertools import*
def f(l):a=[[sum(map(sum,[*s])),k]for k,s in groupby(sorted(l))];return[vfor v in a if v==max(a)]


Try it online!

import Data.Lists
f x=nub\$argmaxes(\e->sum e*countElem e x)x


Returns a list of the rows.

# Charcoal, 25 bytes

ＩΦθ∧⁼κ⌕θι⁼×№θιΣι⌈Ｅθ×№θλΣλ


Try it online! Link is to verbose version of code. Default output format is each row element on its own line and rows double-spaced. Explanation:

  θ                         Input array
Φ                          Filtered where
κ                      Current index
⁼                       Equals
⌕                     First index of
ι                   Current row
θ                    In input array
∧                        Logical And
№                Count of
ι              Current row
θ               In input array
×                 Multiplied by
Σ             Sum of
ι            Current row
⁼                  Equals
⌈           Maximum of
θ         Input array
Ｅ          Mapped over rows
№       Count of
λ     Current row
θ      In input array
×        Multiplied by
Σ    Sum of
λ   Current row
Ｉ                           Cast to string
Implicitly printed


# Mathematica, 48 bytes

Last[SortBy[Gather[m], Total[Flatten[#]] &]][]


or

TakeLargestBy[Gather[m], Total[#, 2] &, 1][[1, 1]]


where (for example)

m = {{1, 2, 3}, {2, 0, 4}, {7, 9, 5}, {6, 3, 0}, {2, 0, 4},
{6, 3, 0}, {2, 0, 4}, {7, 9, 5}};

• You can use shorthand and remove whitespace to save bytes: SortBy[Gather@m,Total@*Flatten][[-1,1]] – Doorknob Jan 1 '19 at 1:56
• This looks like it takes input from a predefined variable, which is not allowed. Submissions have to be full programs or function by default. – Dennis Jan 2 '19 at 0:52
• TakeLargestBy[Gather[m], Total[#, 2] &, 1][[1, 1]] /@ m – David G. Stork Jan 2 '19 at 0:54
• This is not valid; it only returns one of the rows with greatest values rather than all of them. – lirtosiast Jan 2 '19 at 1:50

# JavaScript (ES6), 88 bytes

Outputs an array of Boolean values in appearance order.

m=>m.map(h=o=r=>h=(v=o[r]=~~o[r]+eval(r.join+))<h?h:v)&&Object.keys(o).map(x=>o[x]==h)


Try it online!

# Groovy, 76 bytes

{l->m=l.toSet().sort().collect{it.sum()*l.count(it)}
m.collect{it==m.max()}}


Try it online!

Returns as booleans in sort order

# Scala, 63 bytes

l=>{var m=l.distinct.map(n=>n.sum*l.count(n==))
m.map(m.max==)}


Try it online!

Returns booleans in appearance order

# APL (Dyalog Unicode), 12 bytes

(⌈/=⊢)+.×∘⍴⌸


Try it online!

-2 thanks to Adám. -1 thanks to alternate output format.

• Now comes the challenge of explanation… – Adám Jan 2 '19 at 9:30

# Wolfram Language (Mathematica), 42 bytes

#~Position~Max@#&@(Total[#,2]&/@Gather@#)&


Try it online!

# Python 2, 81 78 bytes

lambda a:{u for u in a if a.count(u)*sum(u)==max(a.count(t)*sum(t)for t in a)}


Try it online!

3 bytes thx to Black Owl Kai.

Given a collection of tuples, the output is a set of those tuples having the desired maximal property.

• 78 bytes – Black Owl Kai Jan 2 '19 at 11:49
• @Black Owl Kai: thx! I missed that... – Chas Brown Jan 2 '19 at 20:29

# Japt, 13 11 bytes

-2 bytes from @Shaggy

ü¬®xx
m¥Urw


Try it online!

• 11 bytes – Shaggy Jan 3 '19 at 10:00
• – Oliver Jan 3 '19 at 15:29

# C# (Visual C# Interactive Compiler), 126 bytes

n=>{var i=n.OrderBy(a=>a.Sum()*n.Count(a.SequenceEqual));return i.Where((a,b)=>i.Take(b).Count(a.SequenceEqual)<1).Reverse();}


Try it online!

Most of this code is spent taking out all duplicate values, since the default comparer for lists do not compare the values inside the lists. That means that I cannot use Distinct(), GroupBy(), or Contains to filter the list.

# K (ngn/k), 17 bytes

{&a=|/a:+//'x@=x}


Try it online!

{ } function with argument x

=x group - form a dictionary in which the keys are rows and the values are lists of their indices in the matrix

x@ index the original matrix with that. the result is again a dictionary with the rows as keys. the values are multiple copies of the corresponding key

+//' sum until convergence each (acts only on the values; keys remain as they are)

a: assign to a

|/ maximum (of the values)

a=|/a a row-to-boolean dictionary of which rows contribute the most

& "where", i.e. which keys correspond to values of 1

# Japt, 11 10 bytes

ü¬ü_xxÃÌmâ


Run it online

# 05AB1E, 10 9 bytes

ês{γOOZQÏ


Explanation:

ê          # Sort and uniquify the (implicit) input list of lists
#  i.e. [[2,0,4],[1,2,3],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]
#   → [[1,2,3],[2,0,4],[6,3,0]]
s         # Swap so the (implicit) input list of lists is at the top again
{        # Sort it
#  i.e. [[2,0,4],[1,2,3],[6,3,0],[2,0,4],[6,3,0],[2,0,4]]
#   → [[1,2,3],[2,0,4],[2,0,4],[2,0,4],[6,3,0],[6,3,0]]
γ       # Group the sorted list of lists
#  i.e. [[1,2,3],[2,0,4],[2,0,4],[2,0,4],[6,3,0],[6,3,0]]
#   → [[[1,2,3]],[[2,0,4],[2,0,4],[2,0,4]],[[6,3,0],[6,3,0]]]
O      # Take the sum of each inner-most lists
#  i.e. [[[1,2,3]],[[2,0,4],[2,0,4],[2,0,4]],[[6,3,0],[6,3,0]]]
#   → [,[6,6,6],[9,9]]
O     # Take the sum of each inner list
#  i.e. [,[6,6,6],[9,9]] → [6,18,18]
Z    # Get the max (without popping the list of sums)
#  i.e. [6,18,18] → 18
Q   # Check for each if this max is equal to the sum
#  i.e. [6,18,18] and 18 → [0,1,1]
Ï  # Filter the uniquified list of lists on truthy values (and output implicitly)
#  i.e. [[1,2,3],[2,0,4],[6,3,0]] and [0,1,1] → [[2,0,4],[6,3,0]]


# Gaia, 10 bytes

ȯẋ_¦Σ¦:⌉=¦


Try it online!

Since Gaia doesn't accept lists through inputs very easily, this is a function that accepts a list from the top from the top of the stack and leaves the result on top (as masks of sorted order).

ȯ           Sort the list
ẋ          Split it into runs of the same element (in this case, runs of the same row)
_¦        Flatten each sublist
Σ¦      Sum each sublist
:⌉    Find the maximum sum
=¦  Compare each sum for equality with the maximum


# J, 16 bytes

[:(=>./)+/^:2/.~


Try it online!

A monadic verb that gives the boolean result in appearance order.

### How it works

[:(=>./)+/^:2/.~
/.~  Self-classify; collect identical rows in appearance order
For each collected rows (= a matrix),
+/^:2       Sum all the elements into one value
[:(=>./)          Compute the boolean vector:
>./             Is the max of the array
=                equal to this element?