Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to check if an array contains the same objects of another array

Tags:

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?

like image 755
FrankZp Avatar asked Feb 18 '13 11:02

FrankZp


People also ask

How do you check if an array includes elements of another array?

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.

How do I check if two arrays have the same contents?

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.


2 Answers

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"); } 
like image 178
Anoop Vaidya Avatar answered Sep 17 '22 15:09

Anoop Vaidya


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]) {  } 
like image 20
Shashank Avatar answered Sep 18 '22 15:09

Shashank