Challenge 2


Check out my approach to the problem.

Logic behind the fast enumeration part: as proper names are in both arrays, we can ignore Capitalized words (proper names) in wordsArray and focus only on lowercase words, so we need to lowercase the names in namesArray and compare these to the words in wordsArray.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *wordsString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
        NSString *namesString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];

        NSArray *wordsArray = [wordsString componentsSeparatedByString:@"\n"];
        NSArray *namesArray = [namesString componentsSeparatedByString:@"\n"];

        NSMutableArray *namesAndWords = [NSMutableArray array];
        for (NSString *name in namesArray){
            for (NSString *word in wordsArray){
                if ([[name lowercaseString] compare:word] == NSOrderedSame ){
                    [namesAndWords addObject:name];
        for (NSString *item in namesAndWords){
            NSLog(@"%@", item);
        NSLog(@"%lu proper names matching words.", [namesAndWords count]);
    return 0;


2015-01-24 21:23:06.160 ProperNames[1616:114706] Al
2015-01-24 21:23:06.162 ProperNames[1616:114706] Alan
2015-01-24 21:23:06.162 ProperNames[1616:114706] Alf
2015-01-24 21:23:06.163 ProperNames[1616:114706] Alison

2015-01-24 21:23:06.278 ProperNames[1616:114706] Wolf
2015-01-24 21:23:06.278 ProperNames[1616:114706] Woody
2015-01-24 21:23:06.278 ProperNames[1616:114706] 294 proper names matching words.
Program ended with exit code: 0