Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7740345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:45:17+00:00 2026-06-01T08:45:17+00:00

I have a NSMutableArray and a NSArray . Both consist of elements that are

  • 0

I have a NSMutableArray and a NSArray. Both consist of elements that are NSDictionarys themselves.
A sample structure of 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 is

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

What I want is to merge these two arrays so that, if any element in NSArray has same objectId as any element in NSMutableArray, element in NSArray should overwrite on element in NSMutableArray.

So in this case 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;
        }
        .
        .
        .
        .
    ]

Only way to this I know is to iterate through both arrays and merge. Is there any better way? Any help will be greatly appreciated.

EDIT:

Following dasblinkenlights suggestion, I did it following way

    -(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]];
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:45:18+00:00Added an answer on June 1, 2026 at 8:45 am

    Since your objectId value can be used as a unique key, you could potentially create an NSMutableDictionary on the side, populate it with NSDictionary objects from the first array using objectId value as the key, go through the second array, do the overwrites, and finally harvest the values of the resultant NSMutableDictionary into your final output.

    Note that this approach might be helpful only if your arrays are relatively long (1000+ items). If you deal with 10..100 items, I wouldn’t bother, and code two nested loops as you suggested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NSMutableArray of NSArray's inside it. This is my current structure: Paused
I'd like to have an NSArray or NSMutableArray that always shows a filtered view
I have an NSMutableArray that is populated with objects of strings. For simplicity sake
I have a NSMutableArray that I need to search for a string and return
I have a NSMutableArray (called listOfSites) that is created and used in classA.m. I
I have an NSMutableArray called message that I've alloc'd in init and release in
I have an NSMutableArray that contains many NSArrays. At a specific (static) index in
I have a simple predicate function that follows as: [totalSentences addObjectsFromArray:[firstLangEx filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@(SELF contains[c]
I have a NSMutableArray named as records and a NSArray . Now I am
Say you have a method that returns a newly generated NSArray instance that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.