Finding maximum numeric value in NSArray

IphoneObjective CNsarray

Iphone Problem Overview


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.

Iphone Solutions


Solution 1 - Iphone

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

Solution 2 - Iphone

NSArray *  test= @[@3, @67, @23, @67, @67];
int maximumValue = [[test valueForKeyPath: @"@max.self"] intValue];
 NSLog(@" MaximumValue = %d", maximumValue);

// Maximum = 67

Solution 3 - Iphone

Here is the swift version

let maxValue =  (numbers.value(forKeyPath: "@max.self") as! Double)

Solution 4 - Iphone

Hope will helpful to you.

NSArray *  arrayOfBarGraphValues = @[@65, @45, @47 ,@87 , @46, @66  ,@77  ,@47  ,@79  ,@78  ,@87  ,@78  ,@87 ];
int maxOfBarGraphValues = [[arrayOfBarGraphValues valueForKeyPath: @"@max.self"] intValue];
NSLog(@" MaximumValue Of BarGraph  = %d", maxOfBarGraphValues);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam S.View Question on Stackoverflow
Solution 1 - IphonesergiobujView Answer on Stackoverflow
Solution 2 - IphonejoanView Answer on Stackoverflow
Solution 3 - IphoneZulqarnain MustafaView Answer on Stackoverflow
Solution 4 - IphoneBuLB JoBsView Answer on Stackoverflow