Compare 2 nsmutablearray and get another object for the third array in ios

I want to compare 2 NSMutableArray and get another object in a third Array . How can i do this?

Array1 can loop an object.

 Array1 = "a", "b","c","d","a","b","c"; Array2 = "a", "b", "c"; 

And then the result

 Array3 = "d"; 

Thank you in advance

+6
source share
4 answers

Use sets for set operations:

 NSSet *set1 = [NSSet setWithArray:array1]; NSMutableSet *set2 = [NSMutableSet setWithArray:array2]; [set2 minusSet:set1]; 
+20
source

You can also try.

 NSMutableArray *array1 = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"1", nil]; NSMutableArray *array2 = [[NSMutableArray alloc]initWithObjects:@"2",@"1", nil]; NSMutableArray *largeArray; NSMutableArray *shortArray; if([array1 count] > [array2 count]){ largeArray = array1; shortArray = array2; } else { largeArray = array2; shortArray = array1; } [largeArray removeObjectsInArray:shortArray]; for (NSString *va in largeArray) { NSLog(@"%@",va); } 
+8
source
 NSMutableArray *gotDiffArry= [[NSMutableArray alloc] init]; for(int i = 0 ; i < FirstArray.count; i++) { if(i < seconArray.count){ if(![seconArray[i] isEqual:firstArray[i]]){ [gotDiffArry addObject:[NSNumber numberWithInt:i]]; } } else { [gotDiffArry addObject:[NSNumber numberWithInt:i]]; } } 

Edition:

 for (int i = 0 ; i < firstArray.count ; i ++) { NSString *search = [firstArray objectAtIndex:i]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF CONTAINS %@", search]; NSMutableArray *temAraay = [secondArray filteredArrayUsingPredicate: predicate]; if(temArray.count >=0 ) { NSLog("%@", [temArray objectAtIndex:0]); } } 
+5
source

I used the following and got the desired results:

 for(int i =0; i<[arraytwo count]; i++) { if (![arrayone containsObject:[arraytwo objectAtIndex:i]]) [arraythree addObject: [arraytwo obectAtIndex:i]]; } NSLog(@"%@",arraythree); 
+4
source

Source: https://habr.com/ru/post/949300/


All Articles