Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding maximum numeric value in NSArray

I have an NSArray of NSNumbers and want to find the maximum value in the array. Is there any built in functionality for doing so? I am using iOS4 GM if that makes any difference.

like image 468
Adam S. Avatar asked Jun 20 '10 18:06

Adam S.


2 Answers

The KVC approach looks like this:

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

or

NSNumber * max = [numbers valueForKeyPath:@"@max.intValue"]; 

with numbers as an NSArray

like image 136
sergiobuj Avatar answered Sep 17 '22 14:09

sergiobuj


NSArray *  test= @[@3, @67, @23, @67, @67]; int maximumValue = [[test valueForKeyPath: @"@max.self"] intValue];  NSLog(@" MaximumValue = %d", maximumValue);  // Maximum = 67 
like image 39
joan Avatar answered Sep 17 '22 14:09

joan