Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Two Arrays

I have two NSArrays, what I'm looking to do is to compare two arrays which contain strings, find the similarities and create the first array again but so they have no similarities.

Just for an example something like.

Two Arrays:

NSArray *arrayOne = [NSArray arrayWithObjects:@"TD1", @"TD2", @"TD3", nil]; NSArray *arrayTwo = [NSArray arrayWithObjects:@"Blah", @"String", @"TD2", nil]; 

Outcome:

NSArray *arrayOne = [NSArray arrayWithObjects:@"TD1", @"TD2", @"TD3", nil];  NSArray *arrayOneCopy = [NSArray arrayWithObjects:@"TD1", @"TD3", nil]; NSArray *arrayTwo = [NSArray arrayWithObjects:@"Blah", @"String", @"TD2", nil]; 
like image 948
Joshua Avatar asked Sep 25 '09 05:09

Joshua


People also ask

How do I compare two 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 you compare if 2 arrays are equal to each other?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

Can we directly compare two array?

Java provides a direct method Arrays. equals() to compare two arrays. Actually, there is a list of equals() methods in the Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is the base of all classes in Java).


1 Answers

NSMutableArray *arrayOneCopy = [NSMutableArray arrayWithArray:arrayOne]; [arrayOneCopy removeObjectsInArray:arrayTwo]; 
like image 155
newacct Avatar answered Oct 08 '22 22:10

newacct