XcodeMania
XcodeMania

Reputation: 305

Compare 2 NSArrays?

Is there a much faster way to compare 2 NSArrays? I need to know if nicks are present in the two arrays and get their index.

look my method i think we can do something more faster

-(void)classPseudo
{
     AppDelegate  *app = [[UIApplication sharedApplication]delegate];
     NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString *fullFileName = [NSString stringWithFormat:@"%@/contactArray.txt", documentsPath];
     NSMutableArray *newOnlineArray = [[NSMutableArray alloc]initWithContentsOfFile:fullFileName];
     NSUInteger count = [newOnlineArray count];
     NSUInteger count2 = [app.messagePseudo count]; 
     for (NSUInteger index = 0; index < count; index++)
    {
        for (NSUInteger index2 = 0; index2 < count2; index2++)
        {

        dict1= [newOnlineArray objectAtIndex:index];
        st1 = [dict1 objectForKey:@"pseudo"];
        dict2= [app.messagePseudo objectAtIndex:index2];
         st2 = [dict2 objectForKey:@"expediteur"];
                if ([st2 isEqualToString:st1]) 
            {
                NSLog(@"YESS %d",index);

                 }
            else {
                NSLog(@"NOOOON");

            }
         }
    }
}

Any Help will be appriated .Thanx in Advance.

Upvotes: 1

Views: 203

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90531

You haven't explained your question very clearly. For example, you haven't explained the data structures involved. (Apparently two arrays of dictionaries.) You haven't said precisely what you want as output. And for those who don't speak French, it's not immediately clear that "nicks" corresponds to "pseudo" in one case and "expediteur" in another. You haven't explained the expected data sets. For example, is each pseudo expected to appear many times or few times in the first array? Same for expediteur in the second.

Anyway, you might build a map of "pseudo" values to index sets. Then, iterate over the second array and look up expediteurs in the map.

NSMutableDictionary* pseudoIndexes = [NSMutableDictionary dictionary];
[newOnlineArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
    NSString* pseudo = [obj objectForKey:@"pseudo"];
    NSMutableIndexSet* indexes = [pseudoIndexes objectForKey:pseudo];
    if (!indexes)
    {
        indexes = [NSMutableIndexSet indexSet];
        [pseudoIndexes setObject:indexes forKey:pseudo];
    }
    [indexes addIndex:idx];
}];

[app.messagePseudo enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
    NSString* expediteur = [obj objectForKey:@"expediteur"];
    NSIndexSet* indexes = [pseudoIndexes objectForKey:expediteur];
    NSLog(@"For expediteur #%d '%@', pseudo indexes %@", idx, expediteur, indexes);
}];

Upvotes: 1

Related Questions