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.
You need
which.min()
since non-zero elements will become FALSE with!m
. – user2390246 – 2017-06-26T16:09:11.563@user2390246 oh, wow, I completely misread the question. Fixed, thank you. – Giuseppe – 2017-06-26T16:33:05.023