Merging two NSArrays consisting of dictionaries based on a parameter in dictionaries

I also have NSMutableArray

a NSArray

. Both are made up of the elements themselves NSDictionarys

. The sample structure for both is as follows:

NSMutableArray
[
    {
        objectId = 4274;
        name = orange;
        price = 45;
        status = approved;
    },
        {
        objectId = 9035;
        name = apple;
        price = 56;
        status = approved;
    },
        {
        objectId = 7336;
        name = banana;
        price = 48;
        status = approved;
    }
    .
    .
    .
    .
]

      

and NSAraay

NSArray
[
    {
        objectId = 4274;
        name = orange;
        price = 106;
        status = not_approved;
    },
        {
        objectId = 5503;
        name = apple;
        price = 56;
        status = approved;
    }
]

      

I want to concatenate these two arrays so that if any element in NSArray

is the same objectId

as any element in NSMutableArray

, element in NSArray

must be overwritten on element in NSMutableArray

.

So, in this case, the final merged array should look like this:

MergedArray
    [
        {
            objectId = 4274;
            name = orange;
            price = 106;
            status = not_approved;
        },
            {
            objectId = 9035;
            name = apple;
            price = 56;
            status = approved;
        },
            {
            objectId = 7336;
            name = banana;
            price = 48;
            status = approved;
        },
           {
            objectId = 5503;
            name = apple;
            price = 56;
            status = approved;
        }
        .
        .
        .
        .
    ]

      

The only way I know is to iterate through both arrays and merge. Is there a better way? Any help would be appreciated.

EDIT

Following dasblinkenlights suggestion, I did it like this

    -(NSMutableArray*)mergeTwoArray:(NSArray*)array1 :(NSArray*)array2
    {
//array1 will overwrite on array2
    NSSet* parentSet = [NSSet setWithArray:array2];

        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        for (NSDictionary *item in parentSet) 
           [dict setObject: item forKey: [item objectForKey:@"objectId"]];


        NSLog(@"initial dictionary is %@",dict);
        for (NSDictionary *item in array1)            
            [dict setObject: item forKey: [item objectForKey:@"objectId"]];

        NSLog(@"final dictionary is %@ with all values %@", dict,[dict allValues]);

        return [NSMutableArray arrayWithArray:[dict allValues]];
    }

      

+3


source to share


2 answers


Since your value objectId

can be used as a unique key, you can create NSMutableDictionary

sideways, fill it with objects NSDictionary

from the first array using the value objectId

as the key, walk through the second array, overwrite, and finally collect the resulting values NSMutableDictionary

into your final output.



Please note that this approach can only be useful if your arrays are relatively long (1000+ elements). If you are dealing with 10..100 points, I would not bother and code two nested loops as you suggested.

+2


source


I would recommend iterating over both arrays and merge, but sort them first. After sorting, you can concatenate the two arrays in O (N) time. For most purposes, this is about as fast as you can get it and requires very little code to do it.

If they are large enough that the sort is a bottleneck, you can get fancy using NSSet

: first put the array of elements (s) into the set and then add the elements of the original array, But you will need to implement the isEqual method on your elements. In this case, it would mean that your elements will no longer be NSDictionary

, but rather a class that inherits from NSDictionary

, but implements the isEqual method for comparing object ID fields.



Because it NSSet

gives amortized constant access time, it would be faster if the arrays are large, since there is no sorting phase.

+1


source







All Articles