The goal is to compare two arrays as and check if they contain the same objects (as fast as possible - there are lots of objects in the arrays). The arrays cannot be checked with isEqual:
as they are differently sorted.
I already tried the solution posted here (https://stackoverflow.com/a/1138417 - see last code snippet of the post by Peter Hosey). But this doesn't work with differently sorted arrays.
The code I'm using now is the following:
+ (BOOL)arraysContainSameObjects:(NSArray *)array1 andOtherArray:(NSArray *)array2 { // quit if array count is different if ([array1 count] != [array2 count]) return NO; BOOL bothArraysContainTheSameObjects = YES; for (id objectInArray1 in array1) { BOOL objectFoundInArray2 = NO; for (id objectInArray2 in array2) { if ([objectInArray1 isEqual:objectInArray2]) { objectFoundInArray2 = YES; break; } } if (!objectFoundInArray2) { bothArraysContainTheSameObjects = NO; break; } } return bothArraysContainTheSameObjects; }
This works, but those are two nested fast enumerations. Is there a way to do a faster comparison?
Use the inbuilt ES6 function some() to iterate through each and every element of first array and to test the array. Use the inbuilt function includes() with second array to check if element exist in the first array or not. If element exist then return true else return false.
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.
As per your code, you are strict to same number of elements and each object of first array should be there in second array and vice versa.
The fastest way would be to sort both the array and compare them.
Ex:
NSArray *array1=@[@"a",@"b",@"c"]; NSArray *array2=@[@"c",@"b",@"a"]; array1=[array1 sortedArrayUsingSelector:@selector(compare:)]; array2=[array2 sortedArrayUsingSelector:@selector(compare:)]; if ([array1 isEqualToArray:array2]) { NSLog(@"both have same elements"); } else{ NSLog(@"both having different elements"); }
How about converting both arrays to sets and comparing them.
NSSet *set1 = [NSSet setWithArray:arr1]; NSSet *set2 = [NSSet setWithArray:arr2];
Compare the two using
if([set1 isEqualToSet:set2]) { }
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