0

I need to compare two arrays (A & B), than for the elements of A that belong also to B I need to set an if statement. Just to explain me better:

if (elementOfArrayA belong AlsoToarrayB) {
        //do something
    }else{
        //do something else
    }

Someone could help me? Thanks

Malloc
  • 15,434
  • 34
  • 105
  • 192
Totka
  • 627
  • 6
  • 24

5 Answers5

2

NSArray has an instance method called containsObject: exactly for this.

For further clarification, check this out.

Community
  • 1
  • 1
Stavash
  • 14,244
  • 5
  • 52
  • 80
2

Use following code to compare two array :

    NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil];
    NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",nil];

     for(int i = 0;i<[array1 count];i++)
        {
            for(int j= 0;j<[array2 count];j++)
            {
                if([[array1 objectAtIndex:i] isEqualToString:[array2 objectAtIndex:j]])
                {

                }  else {

                }
            }
        }
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
  • I need this to set different image on a button inside a custom tableviewcell if one element of the array showed inside the table belong also to another array, but this method doesn't seems to work for me, and my app crash – Totka Aug 11 '13 at 18:45
2

Comparing two arrays:

if([arrayA isEqualToArray:arrayB]){
//the two arrays A and B are equals
}

The code above will test ALL elements of both arrays to check if they fulfill the isEqual test, so no need to for loop the array.

If you want to check wether an element of arrayA is contained in arrayB, use the following method:

id firstCommonObject = [arrayA firstObjectCommonWithArray:arrayB];

if(firstCommonObject != nil){
  //a common object between arrayA and arrayB has been found

}else{
  //no common objects between both arrays
}
Malloc
  • 15,434
  • 34
  • 105
  • 192
  • I need this to set different image on a button inside a custom tableviewcell if one element of the array showed inside the table belong also to another array, but this method doesn't seems to work for me – Totka Aug 11 '13 at 18:43
1
// Method 1 - Simplest method to solve above problem (Use NSArray's containsObject method)
NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"e", nil];
NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",@"f", nil];

for(id i in array1){
    if ([array2 containsObject:i]) {
        // do something
    }
    else {
        // do something else
    }
}

// Method 2 - Another method (Use NSString's isEqualToString method)
NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"e", nil];
NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",@"f", nil];

for(id i in array1){
    for(id j in array2){
        if ([i isEqualToString:j]) {
            // do something
        }
        else {
            //do something else
        }
    }
}
Nitesh Borad
  • 4,583
  • 36
  • 51
0

it may be help you...

-(void)methodFour
{
    NSArray *arr1 = [[NSArray alloc]initWithObjects:@"a2223a",@"ab33b",@"a1acdf",@"ac23c45", nil];
    NSArray *arr11 =  [arr1 sortedArrayUsingSelector:@selector(localizedCompare:)];
    NSLog(@"%@",arr11);

    NSArray *arr2 = [[NSArray alloc]initWithObjects:@"ab33b",@"ac23c45",@"a1acdf",@"a2223a", nil];
    NSArray *arr22= [arr2 sortedArrayUsingSelector:@selector(localizedCompare:)];
    [self firstArray:arr11 secondArray:arr22];
   }
-(void)firstArray:(NSArray *)array1 secondArray:(NSArray *)array2
{
    if ([array1 isEqualToArray:array2])
    {
        NSLog(@"equal");

    }
    else
    {
        NSLog(@"Not equal");

    }
}
karthikeyan
  • 3,821
  • 3
  • 22
  • 45