| Bytes | Lang | Time | Link |
|---|---|---|---|
| 007 | APL Dyalog | 170626T230558Z | Adá |
| 007 | APL | 241204T053233Z | Aaron |
| 055 | JavaScript Node.js | 241204T011438Z | l4m2 |
| 055 | AWK | 241203T200851Z | xrs |
| 031 | Arturo | 240524T044524Z | chunes |
| 025 | Perl 5 pa | 231113T201835Z | Xcali |
| 425 | Vyxal | 231113T195152Z | pacman25 |
| 007 | Uiua | 231113T174801Z | chunes |
| 007 | Japt | 170706T085044Z | Shaggy |
| 011 | CJam | 170626T170127Z | Erik the |
| 016 | q/kdb+ | 170626T215130Z | mkst |
| 051 | Python 2 | 170628T125140Z | Coty Joh |
| 100 | Java 8 | 170628T134825Z | Xanderha |
| 051 | JavaScript ES6 | 170627T133617Z | Craig Ay |
| 064 | Clojure | 170626T162151Z | NikoNyrh |
| 101 | Java OpenJDK 8 | 170627T082758Z | Olivier |
| 145 | Java 8 | 170627T085427Z | Kevin Cr |
| 017 | Brachylog | 170627T080326Z | Leaky Nu |
| 016 | J | 170627T073327Z | Leaky Nu |
| 046 | Retina | 170627T012201Z | Leaky Nu |
| 006 | Pyth | 170626T222833Z | isaacg |
| 048 | Python 3 | 170626T162603Z | Censored |
| 007 | Jelly | 170626T152814Z | hyperneu |
| 068 | Haskell 68 Bytes | 170626T165206Z | Henry |
| 009 | Actually | 170626T182259Z | user4594 |
| 041 | Haskell | 170626T180335Z | nimi |
| 005 | Jelly | 170626T160913Z | Erik the |
| 031 | R | 170626T155453Z | Giuseppe |
| 006 | 05AB1E | 170626T162436Z | Datboi |
| 062 | JavaScript ES6 | 170626T154746Z | Shaggy |
| 048 | Python 2 | 170626T154454Z | 0xffcour |
| 005 | 05AB1E | 170626T162825Z | Erik the |
| 092 | Python 3 | 170626T160155Z | P. Siehr |
| 006 | MATL | 170626T154322Z | Luis Men |
| 018 | V | 170626T155811Z | DJMcMayh |
| 058 | PHP | 170626T154904Z | Jör |
| 062 | Python 2 | 170626T154312Z | ovs |
| 069 | C# | 170626T154047Z | TheLetha |
| 023 | Mathematica | 170626T153138Z | alephalp |
APL (Dyalog), 11 7 bytes
⊃⍒+/0≠⎕
0≠⎕ Boolean matrix where non-zero
+/ sum rows
⍒ get the indices of elements in descending order
⊃ pick the first
APL, 7 chars
⊃⍒+/0≠⎕
Explanation
0≠⎕ ⍝ Non-zero elements of input matrix
+/ ⍝ Sum each row
⍒ ⍝ Order them descending (which is returning the index)
⊃ ⍝ Grab the first
JavaScript (Node.js), 55 bytes
m=>m.map((x,i)=>[(x+',').match(/0,/g),,i]).sort()[0][2]
Unary sorting
,,4
0,,0,,,3
0,,0,,0,,0,,,0
0,,0,,0,,0,,,2
0,,0,,0,,0,,0,,,1
AWK, 55 bytes
{for(i=0;i++<NF;)b[NR]+=0!=$i}END{asort(b);print b[NR]}
{for(i=0;i++<NF;) # columns
b[NR] # rows
+=0!=$i} # sum 0 or 1
END{asort(b); # sort values
print b[NR]} # print last row sum
Arturo, 31 bytes
$=>[max.index map&=>[size&--0]]
Explanation
$=>[] ; a function where input is assigned to &
max.index ; the index of the maximum value in...
map&=>[] ; each row of the input mapped to...
size ; the length of...
&--0 ; the current row without zeros
Vyxal, 34 bitsv2, 4.25 bytes
vT@ÞM
Bitstring:
1000101100100011101101110100011000
optimally encoded solution
Uiua, 7 bytes
⊢⍖/+≠0⍉
⊢⍖/+≠0⍉
⍉ # transpose
≠0 # where is it not equal to zero?
/+ # sum columns
⊢⍖ # index of maximum
Japt, 7 bytes
0-indexed. Takes input as an array of arrays.
mè
bUrw
Explanation
Implicit input of array U.[[0,4,1,0],[0,0,-6,0],[0,1,4,-3],[2,0,0,8],[0,0,0,0]]
mè
Map (m) over U returning the count of truthy (non-zero) elements in each sub-array. Implicitly assign this new array to U.[2,1,3,2,0]
Urw
Reduce (r) array U by getting the greater of the current value and the current element.3
b
Get the first index in U where the element equals that value and implicitly output the result.2
q/kdb+, 25 17 16 bytes
Solution:
(*)(<)sum(+)0=/:
Example:
q)(*)(<)sum(+)0=/:enlist(1;0)
0
q)(*)(<)sum(+)0=/:(0 -1;0 0)
0
q)(*)(<)sum(+)0=/:(1 1 0 0 0;0 0 5 0 0;2 3 0 0 0;0 5 6 2 2)
3
q)(*)(<)sum(+)0=/:(0 4 1 0;0 0 -6 0;0 1 4 -3;2 0 0 8;0 0 0 0)
2
Explanation:
first iasc sum flip 0=/: / ungolfed
/: / each right, apply a function to each item to the right
0= / returns boolean 1b or 0b if item in each list is equal to zero
flip / flip (rotate) the output
sum / sum these up
iasc / return indices if we were to sort ascending
first / take the first one
Notes:
The problem is fairly straightforward, this solution feels overly complicated. As soon as I hit submit I realised the error of my ways.
Bonus:
Here's a k solution that weights in at 16 10 9 bytes - almost exactly the same but 7 bytes shorter due to the fact we don't need brackets when using the k built-ins, and as a result some become shorter than the q keywords (e.g. +/ for sum (would be (+/) in q)).
*<+/+0=/:
Python 2, 51 bytes
def f(x,i=0):print i;x[i].remove(0);f(x,-~i%len(x))
This version removes 0s progressively through the arrays, printing the current index, and crashes when there are no more zeros to remove. Last printed index is the answer.
Python 2, 57 bytes
lambda x,i=0:0in x[i]>x[i].remove(0)and f(x,-~i%len(x))|i
Wanted to try a different approach from what's already here. So here I recursively iterate over the array removing one 0 at a time until the current array no longer has any zeroes - and then output the index of that array.
Java 8, 100 bytes
m->m.indexOf(m.stream().map(z->{z.removeIf(x->x==0);return z;}).max((q,r)->q.size()-r.size()).get())
Explanation
The power of Lists and Streams! (and without the imports, to boot!)
Let's break this little lambda down into chunks:
m.stream().map(z->{z.removeIf(x->x==0);return z;}
We turn our List of Lists (the matrix in the question) into a Stream and go through each element, removing all of those pesky zeroes from each sub-List. We need to explicitly return the sublist each time here, because Stream.map() converts each object in the Stream to whatever the mapping returns, and we don't want to change them.
.max((q,r)->q.size()-r.size()).get()
We go through our newly de-zeroed sublists, and simply check how big they are next to each other, getting us the biggest sublist. The .get() is because the Stream.max() returns an Optional, requiring that extra function call.
m.indexOf()
We take that biggest sublist, and find where it is in the main List, giving us our result!
Notes
This breaks if the outer list is empty, but I'm taking
You may assume that there will only be one row with the most non-zero elements.
to imply that there will always be at least one row. Correct me if I'm wrong.
JavaScript (ES6), 51 Bytes
m=>m.reduce((a,e,i)=>e.filter(x=>x).length>a?i:a,0)
where m is a 2D array and the index returned is 0-indexed
Test cases:
f=
m=>m.reduce((a,e,i)=>e.filter(x=>x).length>a?i:a,0)
console.log(f([[1], [0]]))
console.log(f([[0,-1], [0,0]]))
console.log(f([[1,1,0,0,0], [0,0,5,0,0], [2,3,0,0,0], [0,5,6,2,2]]))
console.log(f([[0,4,1,0], [0,0,-6,0], [0,1,4,-3], [2,0,0,8], [0,0,0,0]]))
Clojure, 64 bytes
This one works also with negative numbers in the input, luckily the same length as the original:
#(nth(sort-by(fn[i](count(filter #{0}(% i))))(range(count %)))0)
Original:
#(last(sort-by(fn[i](count(filter pos?(% i))))(range(count %))))
Java (OpenJDK 8), 119 101 bytes
m->{int i=m.length,M=0,I=0,c;for(;i-->0;){c=0;for(int x:m[i])if(x!=0)c++;if(c>M){M=c;I=i;}}return I;}
Java, that sweet verbose language :)
Thanks for saving 18 bytes, @KevinCruijssen ;)
Java 8, 145 bytes
import java.util.*;m->{int t=0,s=0,i=0,r=0;for(;i<m.size();i++){List l=(List)m.get(i);for(;l.remove(0L););s=l.size();if(s>t){t=s;r=i;}}return r;}
Ugly, but it works..
Explanation:
import java.util.*; // Required import for List
m->{ // Method with List parameter and integer return-type
int t=0,s=0,i=0, // Temp integers
r=0; // Result integer
for(;i<m.size();i++){ // Loop over the List of Lists
List l=(List)m.get(i); // Get the inner List
for(;l.remove(0L);); // Remove all zeros
s=l.size(); // Get the size of the List
if(s>t){ // If this size is larger than the previous
t=s; // Set `t` to this size
r=i; // And set the result to the index of this row
}
} // End of loop
return r; // Return result-integer
} // End of method
Retina, 46 bytes
%M`\b0
m`(?<=(¶.+)*)$
;$#1
O#`.+
!`(?<=^.+;).+
0-indexed. Works with positive and negative integers (and 0). Assumes no leading zeros.
Pyth, 6 bytes
xQh/D0
Instead of finding the row with the most non-zero elements, I find the row with the least zero elements.
/D0: Order (D) by count (/) of zeros (0). Implicitly applied to Q, the input.
h: Take the first, and minimum, element.
xQ: Find the index (x) in the input (Q) of that element.
Python 3, 54 48 bytes
lambda a:a.index(min(a,key=lambda r:r.count(0)))
Shaved off 6 bytes. Old solution:
lambda a:min(range(len(a)),key=lambda i:a[i].count(0))
Jelly, 7 bytes
ċ0$ÞḢi@
ċ0$ÞḢi@ Main link
Þ Sort by
ċ0$ the number of occurences of 0
Ḣ Take the first element
i@ Index in the original array
Haskell - 69 68 Bytes
Saved one byte thanks to Siracusa!
Rows are zero indexed
g=filter
m y=head$g((==maximum y).(y!!))[0..]
f=m.map(length.g(0/=))
Usage
f [[1,1,0,0,0],[2,3,0,0,0],[0,5,6,2,2],[1,1,1,1,1]]
Actually, 9 bytes
0@♀cñ♂RmN
Explanation:
0@♀cñ♂RmN
0@♀c count zeroes in each row
ñ♂R enumerate and reverse each row (each row becomes [count, index] pair)
m minimum
N last element (the index)
Haskell, 46 42 41 bytes
snd.minimum.(`zip`[1..]).map(filter(==0))
How it works
map -- for each row
(filter(==0)) -- collect the 0s
(`zip`[1..]) -- pair with row index (<#0s>, <index>)
minimum -- find the minimum
snd -- extract index from pair
Jelly, 5 bytes
TL$€M
1-indexed.
So many 5-byte versions...
TL$€M, T€L€M, TJ$€M, T€J€M, ¬¬Ṣ€M, ṠAṢ€M, ṠAS€M, AṠṢ€M, AṠS€M, ¬ċ€0M, ...
R, 31 bytes
pryr::f(which.min(rowSums(!m)))
returns an anonymous function which takes a matrix:
function(m)which.min(rowSums(!m))
rowSums sums the rows, with !m transforming 0 to 1 and everything else to 0. which.min returns the 1-based index of the first row which contains the min sum (i.e., which row has the fewest zeros).
05AB1E, 8 6 bytes
ΣĀO}θk
-2 Bytes thanks to Erik the Outgolfer
Explanation
ΣĀO}θk
Σ } # Sort input by following code
Ā # Is element not 0? (vectorized)
O # Sum
θk # Get index of "largest" element
# Implicit print
JavaScript (ES6), 62 bytes
0-indexed. Takes a 2D array as input.
a=>(a=a.map(x=>x.filter(y=>y).length)).indexOf(Math.max(...a))
Python 2, 64 55 52 48 bytes
- Thanks to @Rod for shaving 9 bytes!!: count
0s and usemin()instead ofmax() - @Rod saved yet another 3 bytes: use
input()instead ofdef - @ovs saved 4 bytes: use of
lambdaand hash-map
lambda x:x.index(min(x,key=lambda n:n.count(0)))
Python 3, 92 bytes
def f(x):
for e in x:
e.sort()
y=x[:]
y.sort()
return x.index(y[-1])
First sort each row such that the entrys are [0,0,..,0,x,x,x] then sort the whole matrix, so that the last entry in y is the row we are looking for.
The copy y=x[:] is necessary, since .sort() works inplace, hence we don't know the original index after sorting.
I appreciate any help how to golf this solution further. Most bytes are lost because of the whitespaces in each line. The code itself is only 68 bytes long.
MATL, 6 bytes
!gs&X>
Input is a matrix, with ; as row separator.
Try it online! Or verify all test cases.
Explanation
! % Transpose
g % Logical: convert non-zeros to 1
s % Sum of each column, or sum of row if there's a single row
&X> % Arg max. Implicitly display
V, 18 bytes
òø0
jòÚDuu/"
dGؾ
Unlike most V answers, this is 0-indexed.
00000000: f2f8 300a 6af2 da44 7575 2f12 220a 6447 ..0.j..Duu/.".dG
00000010: d8be ..
Not bad for a language with no numeric support! ;P
I also have discovered that the uppercase variant of the count command, that is Ø, is horribly broken.
C#, 69 bytes
using System.Linq;m=>m.IndexOf(m.OrderBy(r=>r.Count(n=>n!=0)).Last())
Takes a List<int[]> as input and returns the 0-indexed result.
Mathematica, 23 bytes
Ordering[Count@0/@#,1]&