Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C - How to compare arrays and extract the difference?

Possible duplicate: comparing-two-arrays

I have two NSArray and I'd like to create a new Array with objects from the second array but not included in the first array.

Example:  NSMutableArray *firstArray = [NSMutableArray arrayWithObjects:@"Bill", @"Ben", @"Chris", @"Melissa", nil]; NSMutableArray *secondArray = [NSMutableArray arrayWithObjects:@"Bill", @"Paul", nil];  The resulting array should be:   [@"Paul", nil]; 

I solved this problem with a double loop comparing objects into the inner one.

Is there a better solutions ?

like image 332
marione Avatar asked Oct 30 '09 11:10

marione


People also ask

How do I compare two arrays of arrays?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.

How do I compare two arrays to each other?

Programmers who wish to compare the contents of two arrays must use the static two-argument Arrays. equals() method. This method considers two arrays equivalent if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equivalent, according to Object.


1 Answers

[secondArray removeObjectsInArray:firstArray]; 

This idea was taken from another answer.

like image 68
Jean Regisser Avatar answered Sep 20 '22 03:09

Jean Regisser