Question: I'm currently learning Objective-C. The book that I'm learning from has given me a challenge, where I'm given two arrays and I'm supposed to create a program that would show the objects that are listed in both of the Arrays.

I've set everything up, the only part I'm terribly stuck on is comparing both Arrays and returning what comes up in both of them. They are both long lists, and all my attempts end up in nothing being shown up at all or a crash.

Any help on how to do this would be great.

Edit: This is what I've done so far... I can't quite give examples of what I've tried before because I would use things like isEqualToString:, predicateWithFormat:, and after it didn't work I would delete the code.

I apologize if my code contains obvious errors, let me mention again I'm new. 

{
 @autoreleasepool {

```
    // Read in a file as a huge string (ignoring the possibility of an error)
    NSString *nameString =
    [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                              encoding:NSUTF8StringEncoding
                                 error:NULL];
    // Read in a file for words
   NSString *wordString =
    [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
    encoding:NSUTF8StringEncoding
    error:NULL];

    // Break it into an array of strings
    NSArray *namesArray = [nameString componentsSeparatedByString:@"\n"];

    // Break words into an array of strings
    NSArray *wordArray = [wordString componentsSeparatedByString:@"\n"];

    // Go through the array one string at a time
    for (NSString *n in namesArray) {

        // Here is where I'm at

    }
}

```

}

Answer: 
There are lots of approaches. A straightforward one, but not very efficient:

```
NSArray* array1 = /* ... */;
NSArray* array2 = /* ... */;
NSMutableArray* common = [NSMutableArray array];
for (id obj in array1)
{
    if ([array2 containsObject:obj])
        [common addObject:obj];
}

```

Given that the arrays are large, that repeated look-up using `-contains:` will be expensive. It has to search through potentially all of `array2` for each item in `array1`.

That sort of look-up is more efficient using a set, so you might do:

```
NSArray* array1 = /* ... */;
NSArray* array2 = /* ... */;
NSSet* set = [NSSet setWithArray:array2];
NSMutableArray* common = [NSMutableArray array];
for (id obj in array1)
{
    if ([set containsObject:obj])
        [common addObject:obj];
}

```

This could be made simpler and probably faster by using set operations. The task at hand is essentially set intersection (assuming that you don't care about the order of the resulting collection of common items). So, you could do:

```
NSArray* array1 = /* ... */;
NSArray* array2 = /* ... */;
NSSet* set1 = [NSSet setWithArray:array1];
NSMutableSet* common = [NSMutableSet setWithArray:array2];
[common intersectSet:set1];

```

If you're looking for a one-liner, you can do it with `NSPredicate` although, again, it may not be terribly efficient:

```
NSArray* array1 = /* ... */;
NSArray* array2 = /* ... */;
NSArray* common = [array1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self IN %@", array2]];

```
Here is pseudo code hope it will help you to understand how you have to work: 

```
ArrayA=[1,2,3,4];
ArrayB=[2,3,4,5];

for (int i =0; i< [ArrayA count]; i++)
{
    if ([[ArrayA objectAtIndex:i] isEqual: [ArrayB objectAtIndex:i]])
    {
        NSLog(@"the same");
       [<add same object> to ResultArray];
    }
}

```