I have some array, and I need to find minimal non zero integer in each row. Is there a way of doing it with min(by:)?
for example
var row = [0,0,0,0,0,0,0,0,1,3,5,6,9]
so I need to get 1
by doing row.min(
) I always get 0.
I was told that I can do it with min{by:}
but I don't fully understand the syntax.
You can filter the array for desired values, and use Array.min()
method, like so
row.filter{ $0 > 0 }.min()
Or following will only work if array has ordered numbers
row.first(where: { $0 > 0 })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With