0

Its really a simple question but I want a short way to compare two array to get index of containing object. For example we have two arrays....

NSArray *array1=@[@"b",@"a",@"c"];
NSArray *array2=@[@"c",@"b",@"a"];

After comparison from array2 to array1, I want the index of the containing object in array1.

I tried to check the this link but I didn't get ans as I expected Fastest way to check if an array contains the same objects of another array

9
  • if you just want to compare identity than use [array indexOfObject:obj1] Commented May 14, 2014 at 12:02
  • 1
    possible duplicate of Fastest way to check if an array contains the same objects of another array Commented May 14, 2014 at 12:04
  • But first i need to compare the two array and and when i will get the same object then I need to find out the index of that object. Commented May 14, 2014 at 12:08
  • So you want to get the objects that are present in array1 and array2 and then get the index of these objects in array1? Or in both arrays? Commented May 14, 2014 at 12:11
  • @DivyaBhalodiya: It isn't duplicate, I already checked the ans in stackoverflow.com/questions/14935233/… and also mention above... I want index of object after comparing the two arrays. Thanks Commented May 14, 2014 at 12:11

3 Answers 3

1

For getting indexes of objects in array1 which are also present in array2, you can use:

NSIndexSet* indexes = [array1 indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return [array2 containsObject:obj];
}];
[indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
    NSLog(@"Index is %u", idx);  //do whatever you need to do with the index
}];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Michal. This answer is what i expected. Short and simple. Thanks for helping..God bless you..
0

Use following method to get compare index from array 1 :

    NSMutableArray *arrayFirst = [[NSMutableArray alloc] initWithObjects:@"b",@"a",@"c", nil];
    NSMutableArray *arraySecond = [[NSMutableArray alloc] initWithObjects:@"c",@"b",@"a", nil];
    NSMutableArray *arrayComparedIndex = [[NSMutableArray alloc] init];

    for(int i =0; i<[arrayFirst count]; i++)
    {
         if ([arraySecond containsObject:[arrayFirst objectAtIndex:i]])
         {
                NSLog(@"index - %d",i);
                [arrayComparedIndex addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    NSLog(@"arraythree - %@",arrayComparedIndex);

3 Comments

Thanks Divya But please could you tell me the way without using for loop. Is it possible?? I am finding the way without using loop. Nice solution Thanks..
@AG.IS, how do you mean without loop? there is no solution for your problem in O(1) time.
I mean i want to avoid For loop or you can say that i want short line ans. Like Michał Ciuba.
-2
for (int i = 0; i < array1.count; i++) {
        NSString *s = array1[i];
        NSInteger anIndex = [array2 indexOfObject:s];

        NSLog(@"Index of %@ is: %d", s, anIndex);

        if (NSNotFound == anIndex) {
            NSLog(@"not found");
        }
    }

3 Comments

Its working only in case , when both array have same number of object. May be its not working, when number of objects are not same for both array. Its give crash when array1 count is greater that the array2 count.
It is a simple answer to the simple question, of course you can do length checks also but that was not part of the question and is not that difficult to figure out
Thanks @ClemensL, But is it any shortest trick for this situation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.