Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the longest string within an NSArray

I have an NSArray filled with 200,000 words and I don't know the length of each word. I need to know what is the maximum length of a word contained in that array.

For example, if my array is {"dog","person","amazing"} The maximum length of a word contained in this array would be 7 ("amazing")

How would I do this?

like image 742
Markito De La Cruz Avatar asked Jul 23 '13 16:07

Markito De La Cruz


1 Answers

Besides all methods that imply iterating over the array, you can easily do this with valueForKeyPath:, using the @max collection operator:

NSNumber* maxLength= [array valueForKeyPath: @"@max.length"];
like image 115
Ramy Al Zuhouri Avatar answered Nov 10 '22 05:11

Ramy Al Zuhouri