1

I have a NSArray and want to know the maximum and min integers.

I currently use:

int max = [[array valueForKeyPath:@"@max.intValue"] intValue];
int min = [[array valueForKeyPath:@"@min.intValue"] intValue];

but now I want to know the minimum and maximum without the zeros entering (arrays only have positive values​​)

ex:
array=[3, 6 ,7 , 13, 86, 45, 0]
min=3
max=86

Is there any method to achieve this?

Pedro
  • 487
  • 1
  • 5
  • 18

1 Answers1

6
int xmin = INTMAX_MAX;
for (NSNumber *num in numbers) {
    int x = num.intValue;
    if (x < xmin && x!= 0) xmin = x;
}
Salavat Khanov
  • 4,100
  • 2
  • 15
  • 27