如何从多个数组中提取唯一对象

vik*_*mar 4 objective-c nsarray ios5

编辑:
我有两个不同的数组与一些重复的字符串,我想创建一个只有唯一字符串的新数组.

例如,取这两个数组:

NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil]; 
NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",nil];

// Result should be an array with objects "b", and "d" 
// since they are the only two that are not repeated in the other array.
Run Code Online (Sandbox Code Playgroud)

lna*_*ger 6

编辑:

// Your starting arrays
NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil]; 
NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",nil];

// Create two new arrays that only contain the objects 
// which are not in the other array:
NSMutableArray *uniqueElementsInArray1 = [array1 mutableCopy];
[uniqueElementsInArray1 removeObjectsInArray:array2];

NSMutableArray *uniqueElementsInArray2 = [array2 mutableCopy];
[uniqueElementsInArray2 removeObjectsInArray:array1];

// Combine the two arrays.
// Result contains objects @"b" and @"d":
NSArray *result = [uniqueElementsInArray1 arrayByAddingObjectsFromArray:uniqueElementsInArray2];
Run Code Online (Sandbox Code Playgroud)