4

EDIT:
I have two different arrays with some repeated strings and i want to create a new array with the only the unique strings.

For instance, 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.
2
  • 1
    possible duplicate of Remove all strings with duplicates in an NSArray Commented Jun 5, 2012 at 6:30
  • @JoshCaswell FYI, this is not a duplicate since he wants to find objects that are unique across both arrays while the linked question is within one array. Commented Jun 24, 2014 at 21:38

4 Answers 4

6

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];
Sign up to request clarification or add additional context in comments.

3 Comments

NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil]; NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",nil];
So you have two arrays and want to find the strings which only appears in one of the arrays? That seems a little different from what you asked in your question.
Great! Welcome to SO (Stack Overflow)! When people answer your questions, be sure to click the check mark at the top left of the answer so that we get credit for helping you. It is also good to click the up arrow on all of the answers that help, along with the one that you accept.
1

For this you just declare one another temp NSMutableArray . Retrieve whatever data u have from your original array say objectArray. Check whether the temp array have that or not and put it into the temp array. Just refer following code:

    for(NSString *str in objectArray)
    {

        if(![tempArray containsObject:str])
        {

            [tempArray addObject:str];

        }
    }

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

Comments

1

You can use NSSet as a filter (think of Venn Diagrams in your 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 (eschews NSSet, but I would not seat this code opposite the Queen of England at a formal dinner -- it 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 the questioner's question, but to get an array with duplicates removed (i.e., where each element is unique), similar magic can be done:

//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]

As per the Apple Docs (emphasis added):

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

UPDATE: And see here for a similar question: Remove all strings with duplicates in an NSArray

2 Comments

this doesn't answer the original question but was exactly what i was looking for. A+++
Thank you for catching that! Fixed; and I'm glad it helped.
-2

use Set as a filter, example:

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

1 Comment

This question is asking about Objective-C.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.