Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find minimal non zero INT in an array in swift 5.0?

Tags:

arrays

swift

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.

like image 865
Oleksii Avatar asked Nov 30 '22 14:11

Oleksii


1 Answers

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 })
like image 150
AamirR Avatar answered Dec 19 '22 12:12

AamirR