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 ?
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.
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.
[secondArray removeObjectsInArray:firstArray];
This idea was taken from another answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With