How to extract unique objects from multiple arrays

EDIT:
I have two different arrays with multiple duplicate rows, and I want to create a new array with single unique rows.

For example, take these two arrays:

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. 
+4
source share
4 answers

EDIT:

 // 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]; 
+6
source

To do this, you simply declare another NSMutableArray parameter. Get all the data that your source array says objectArray. Check if the temp array has this or not, and put it in the temp array. Just write the following code:

  for(NSString *str in objectArray) { if(![tempArray containsObject:str]) { [tempArray addObject:str]; } } 

After that, u can continue to use tempArray or put tempArray in objectArray if you want to use objectArray further. I think this should work for you.

+1
source

You can use NSSet as a filter (think of Venn diagrams in the head):

 NSArray *array1 = @[@1,@2,@3,@4,@2,@3]; NSArray *array2 = @[@3,@4,@5,@6,@4,@6]; NSSet *set1 = [NSSet setWithArray:array1]; // [1,2,3,4] NSSet *set2 = [NSSet setWithArray:array2]; // [3,4,5,6] 
METHOD 1 (my favorite):
 NSMutableSet *mSet1 = [set1 mutableCopy]; NSMutableSet *mSet2 = [set2 mutableCopy]; [mSet1 minusSet:set2]; // mSet1 = [1,2] [mSet2 minusSet:set1]; // mSet2 = [5,6] [mSet1 unionSet:mSet2]; // mSet1 = [1,2,5,6], only the unique elements. // Now just put it in an immutable collections with a self-docu name... NSArray *arrayOfUniqueness = [setOfUniqueElementsOnly allObjects]; 
METHOD 2 (more explicit test, no need for Venn diagrams):
 NSSet *setOfObjsUniqueTo1 = [set1 objectsPassingTest:^BOOL(id _Nonnull obj, BOOL * _Nonnull stop) { return ![set2 containsObject:obj]; }]; // [1,2] NSSet *setOfObjsUniqueTo2 = [set2 objectsPassingTest:^BOOL(id _Nonnull obj, BOOL * _Nonnull stop) { return ![set1 containsObject:obj]; }]; // [5,6] NSMutableSet *oneSetToRuleThemAll = [NSMutableSet setWithSet:setOfObjsUniqueTo1]; // [1,2] [oneSetToRuleThemAll unionSet:setOfObjsUniqueTo2]; // [1,2,5,6] // Or as an array: NSArray *anotherArrayOfUniqueness = [oneSetToRuleThemAll allObjects]; 
METHOD 3 ( NSSet not worth it, but I would not put this code opposite the Queen of England at an official dinner - this is inelegant):
 NSMutableArray *mArray1 = [NSMutableArray new]; NSMutableArray *mArray2 = [NSMutableArray new]; NSIndexSet *uniqueIndexes1 = [array1 indexesOfObjectsPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { return ![array2 containsObject:obj]; }]; // [0,1,4] (b/c @1 and @2 are unique to array1) [uniqueIndexes1 enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) { [mArray1 addObject:array1[idx]]; }]; // @[@1,@2,@2] NSIndexSet *uniqueIndexes2 = [array2 indexesOfObjectsPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { return ![array1 containsObject:obj]; }]; // [2,3,5] (b/c @5 and @6 are unique to array2) [uniqueIndexes2 enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) { [mArray2 addObject:array2[idx]]; }]; // @[@5,@6,@6] NSArray *unionArray = [array1 arrayByAddingObjectsFromArray:array2]; // @[@1,@2,@2,@5,@6,@6] NSArray *yetAnotherArrayOfUniqueness = [[NSSet setWithArray:unionArray] allObjects]; // @[@1,@2,@5,@6] 

Not a question of the questionnaire, but to remove an array with duplicates (i.e. where each element is unique), similar magic can be performed:

 //given... NSArray *arr1 = @[@"a", @"b", @"c"]; NSArray *arr2 = @[@"b", @"c", @"d"]; //...make a single array to rule them all: NSArray *temp = [arr1 arrayByAddingObjectsFromArray:arr2]; //[a,b,c,b,c,d] //Make an NSSet from the two: NSSet *filterSet = [NSSet setWithArray:temp]; // Set has: a,b,c,d //Finally, transmogrify that NSSet into an NSArray: NSArray *arrayOfUniqueness = [filterSet allObjects]; // [a,b,c,d] 

According to Apple Docs (highlighted by me):

+ setWithArray: Creates and returns a set containing a collection of uniqued objects contained in this array.

UPDATE: And look at a similar question here: Delete all duplicate rows in NSArray

+1
source

Use Set as a filter, for example:

 String[] arr = {"a","a","b"}; Object[] uniqueArr = (Object[])new HashSet<String>(Arrays.asList(arr)).toArray(); 
-1
source

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


All Articles