1

I would like remove every object from array that's value for the name key can be find in array2. I've tried it with a loop, but unfortunately my implementation doesn't worked, the if and else statement logged out every objects.

This is what I would like to achieve:

 NSArray *array = @[
                       @{
                           @"type" : @"2",
                           @"name" : @"apple"
                           },
                       @{
                           @"type" : @"1",
                           @"name" : @"peanut"
                           },
                       @{
                           @"type" : @"3",
                           @"name" : @"orange"
                           },
                       @{
                           @"type" : @"1",
                           @"name" : @"cheese"
                           },
                       ];

NSArray *array2 = @[
                   @{
                       @"type" : @"2",
                       @"name" : @"apple"
                       },
                   @{
                       @"type" : @"1",
                       @"name" : @"cheese"
                       },
                   @{
                       @"type" : @"3",
                       @"name" : @"orange"
                       },
                   ];


// new array from array and array2, that contains the unique objects from array

NSArray *newArray = @[

                   @{
                       @"type" : @"1",
                       @"name" : @"peanut"
                       },

                   ];

I'm not sure that it's a good approach so I would be very happy if somebody could show an example how to do it with any different technique. It doesn't matter if it has to use mutable or normal arrays, I just tried it with mutable first.

This is what we tried:

NSMutableArray *newArray = [self.array1 mutableCopy];

for (PFObject * object in newArray) {

    PFObject *placeholderObject = object;

    for (PFObject *object2 in self.array2) {

        if ([placeholderObject[@"name"] isEqualToString:object2[@"name"]]) {

            [self.array1 removeObject:object];

            NSLog (@"EXISTING OBJECT FOUND %@", object);
        } else {


            NSLog(@"UNIQUE OBJECT FOUND %@", idO[@"hirCime"]);

        }


    }


} 
5
  • 1
    Why dont you add one more key called id and give unique number,so that you can compare that unique number to remove the objects. Else you have to compare two keys,which is more complicated!!! Commented Sep 23, 2015 at 13:53
  • 1
    There is a containsObject: method that you can use to compare objects between two arrays Commented Sep 23, 2015 at 13:55
  • @MSU_Bulldog I tried containsObject and it didn't work with key values of the object. Commented Sep 23, 2015 at 14:00
  • @Mr.T I can't do that, because I don't know what will be in the array, therefore I can't give them a number properly, array's content is not pre-defined. Commented Sep 23, 2015 at 14:02
  • @bushiko you can use NSSet instead of NSArray and then use subtract method of it, which subtracts all similar objects in A and B and results remaining unique objects. Commented Sep 23, 2015 at 14:32

2 Answers 2

2
NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
for (NSDictionary *objDic1 in array) {
    for (NSDictionary *objDic2 in array2) {
        if ([objDic1[@"name"] isEqualToString:objDic2[@"name"]]) {
            [arr removeObject:objDic1];
        }
    }
}
NSArray *resultArr = (NSArray*)arr;
Sign up to request clarification or add additional context in comments.

Comments

2

It's possible to do this by removing elements if you want to, but you have to make sure you're not mutating the array as you iterate through it. Here's an example (based on NSDictionary rather than PFObject...but similar logic):

NSMutableArray *newArray = [array1 mutableCopy];
for (NSDictionary * object in array1) {
    NSDictionary *placeholderObject = object;
    for (NSDictionary *object2 in array2) {
        if ([placeholderObject[@"name"] isEqualToString:object2[@"name"]]) {
            [newArray removeObject:object];
            NSLog (@"EXISTING OBJECT FOUND %@", object);
            break;
        } else {
            NSLog(@"UNIQUE OBJECT FOUND %@", object);
        }
    }
}

2 Comments

Do you think it would be better to do this by adding the unique objects to another array? I would like to do most efficient version.
Adding is probably better. I posted this so you could compare a working implementation that was close to the approach you were already using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.