Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Should array3 just be [ "d" ], or should it be [ "b", "d" ], your question isn't clear about what 'diff object' means? –  Wain Jul 12 '13 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)? –  Maarten Jul 12 '13 at 9:26
    
@Wain: i edited my question –  NGOT Jul 12 '13 at 9:28
    
@Maarten: Yes, it is a object –  NGOT Jul 12 '13 at 9:28
add comment

4 Answers

Use sets for set operations:

NSSet *set1 = [NSSet setWithArray:array1];
NSMutableSet *set2 = [NSMutableSet setWithArray:array2];
[set2 minusSet:set1];
share|improve this answer
1  
+1 @NGOT using NSSet is efficient. –  βhargavḯ Jul 12 '13 at 9:45
    
I trying above code but in console set2 always = 0 –  NGOT Jul 12 '13 at 9:48
    
@NGOT, are you using really string literals as in the question, or some objects of a custom class? –  vikingosegundo Jul 12 '13 at 9:49
1  
@NGOT: You have to swap array1 and array2 because in this answer, array2 is the "larger" array. –  Martin R Jul 12 '13 at 9:52
    
@MartinR Oh, sorry for expecting people to know highschool maths. I apologize. </sarcasm> - and thanks for explaining that to OP. –  user529758 Jul 12 '13 at 9:53
show 3 more comments

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);
}
share|improve this answer
    
HOw can determine that which one is large and which one is short ? –  Ranju Patel Jul 12 '13 at 9:38
    
@RanjuPatel like you have array in which you want to check the duplicates (the duplicates are also in the array). –  Bunty M Jul 12 '13 at 9:45
    
really nice and smart answer bro :) –  Ranju Patel Jul 12 '13 at 10:08
    
@vikingosegundo & ranju thanks good luck. –  Bunty M Jul 12 '13 at 10:17
add comment

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);
share|improve this answer
    
Probably more efficient than the set approach (for smaller arrays, at least). –  Hot Licks Jul 12 '13 at 11:52
    
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. –  Ankit Mehta Jul 12 '13 at 13:05
    
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. –  Hot Licks Jul 12 '13 at 14:55
add comment
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]); 
 }
}
share|improve this answer
    
what type of differentIndex? –  NGOT Jul 12 '13 at 9:37
    
This is too iterative and time consuming if arrays are large. Not an efficient way. –  βhargavḯ Jul 12 '13 at 9:42
    
Thanks but i only want to get diff object not all object. –  NGOT Jul 12 '13 at 9:46
    
@NGOT- then use only else part of Array ...:) –  Ranju Patel Jul 12 '13 at 9:47
    
@NGOT-check my edited answer :) –  Ranju Patel Jul 12 '13 at 10:07
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.