6

I want to compare 2 NSMutableArray and get different object into third Array. How can i do that ?

Array1 can loop object .

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

And then result

Array3 = "d";

Thanks in advance

3
  • Should array3 just be [ "d" ], or should it be [ "b", "d" ], your question isn't clear about what 'diff object' means? Commented Jul 12, 2013 at 9:26
  • Are the objects strings? Also, are you looking for equality (the value is the same) or identity (which objects in array1 occupy the same memory as the objects in array2)? Commented Jul 12, 2013 at 9:26
  • @Maarten: Yes, it is a object Commented Jul 12, 2013 at 9:28

4 Answers 4

21

Use sets for set operations:

NSSet *set1 = [NSSet setWithArray:array1];
NSMutableSet *set2 = [NSMutableSet setWithArray:array2];
[set2 minusSet:set1];
Sign up to request clarification or add additional context in comments.

6 Comments

@NGOT, are you using really string literals as in the question, or some objects of a custom class?
@NGOT: You have to swap array1 and array2 because in this answer, array2 is the "larger" array.
@MartinR Oh, sorry for expecting people to know highschool maths. I apologize. </sarcasm> - and thanks for explaining that to OP.
Array1 is array response from server, and array2 is array copy from Array1. I want after server reponse data to array1, I compare array1 and array2, if in Array2 dont have object which from Array1, reloadTable.
@H2CO3 This is the best and the most helpful answer I've ever seen! So lovely! Thanks. (:
|
7

You Can try this too.

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);
}

2 Comments

HOw can determine that which one is large and which one is short ?
@RanjuPatel like you have array in which you want to check the duplicates (the duplicates are also in the array).
5
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]];
    }
}

EDITED:

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]); 
 }
}

4 Comments

what type of differentIndex?
This is too iterative and time consuming if arrays are large. Not an efficient way.
Thanks but i only want to get diff object not all object.
@NGOT- then use only else part of Array ...:)
4

I have 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);

3 Comments

Probably more efficient than the set approach (for smaller arrays, at least).
I am using it for bigger arrays too. It may take some time for bigger array, but i am doing it in background so works fine.
Yeah, it's just that I haven't gone through a "big O" estimation for the various schemes. Probably the set approach is a hair more efficient for large sets, but you likely have to be in the thousands of elements for it to be significant.

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.