I haven’t benchmarked any of the solutions thus far, but here’s a way to solve it without a nested loop (well, at least not explicitly!)
I’m curious (but too tired) to find out if this is faster or slower than the solutions that nest (and therefore iterate) over the loops manually. I.e., I realize that the indexOfObject message is doing some sort of search, but wonder if it is more efficient than a brute search through the list.
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
NSString *wordsString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
NSArray *nameArr = [nameString componentsSeparatedByString:@"\n"];
NSMutableArray *wordsArr = (NSMutableArray*)[wordsString componentsSeparatedByString:@"\n"];
for(NSString* name in nameArr)
// see if there is a lower case version
NSString* lowerCaseName = [name lowercaseString];
NSInteger foundIndexOfLowerCaseWord = [wordsArr indexOfObject:lowerCaseName];
NSLog(@%d "%@ %@", ++count, name, [wordsArr objectAtIndex:foundIndexOfLowerCaseWord]);
The other way to solve it without doing double for loops that came to my mind is to create a function inside the first loop (that accepts an array and a string and then does a comparison inside the function and returns something)