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

Community
  • 1
  • 1
AG.29
  • 230
  • 1
  • 20
  • if you just want to compare identity than use `[array indexOfObject:obj1]` – Argent May 14 '14 at 12:02
  • 1
    possible duplicate of [Fastest way to check if an array contains the same objects of another array](http://stackoverflow.com/questions/14935233/fastest-way-to-check-if-an-array-contains-the-same-objects-of-another-array) – Divya Bhaloidiya May 14 '14 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. – AG.29 May 14 '14 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? – Michał Ciuba May 14 '14 at 12:11
  • @DivyaBhalodiya: It isn't duplicate, I already checked the ans in http://stackoverflow.com/questions/14935233/fastest-way-to-check-if-an-array-contains-the-same-objects-of-another-array and also mention above... I want index of object after comparing the two arrays. Thanks – AG.29 May 14 '14 at 12:11
  • Yes exactly right @MichałCiuba – AG.29 May 14 '14 at 12:14
  • @AG.IS: "I want index of object after comparing the two arrays" what does that even mean? For every item in array1 check with `[array1 indexOfObject:item]` if it is present in array2 and if it is just use that index – Argent May 14 '14 at 12:33
  • @Argent: It means I have two arrays which contains same objects. So I want to compare each other and when i will find the objects which are same in both array at that situation i want the index of the found object from array 1. – AG.29 May 14 '14 at 12:41
  • @AG.IS: you got plenty of answers to do just that – Argent May 14 '14 at 13:00

3 Answers3

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
}];
Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
  • Thanks @Michal. This answer is what i expected. Short and simple. Thanks for helping..God bless you.. – AG.29 May 15 '14 at 05:42
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);
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
  • 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.29 May 14 '14 at 12:49
  • 1
    @AG.IS, how do you mean _without loop_? there is no solution for your problem in `O(1)` time. – holex May 14 '14 at 12:56
  • I mean i want to avoid For loop or you can say that i want short line ans. Like Michał Ciuba. – AG.29 May 14 '14 at 13:03
-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");
        }
    }
ClemensL
  • 404
  • 2
  • 10
  • 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. – Divya Bhaloidiya May 14 '14 at 12:07
  • 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 – ClemensL May 14 '14 at 12:13
  • Thanks @ClemensL, But is it any shortest trick for this situation – AG.29 May 14 '14 at 12:19