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

  • SEARCH
  • Home
  • 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 7195935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:40:47+00:00 2026-05-28T20:40:47+00:00

I have two NSArray objects and I want to add the two arrays together

  • 0

I have two NSArray objects and I want to add the two arrays together in a component wise fashion such that the int in array1 at index i is added to the int in array 2 at index i with the result added to a new array. Is there a faster way to do this than with standard for for (int i=0; i<[array1 count]; i++) type methods? For example, can you use c arrays, fast enumeration, blocks? I am particularly interested because I would like to add x number of arrays containing large number of objects. My current code is as follows:

    NSArray *array1 = [NSArray arrayWithObjects:
                   [NSNumber numberWithInt:1],
                   [NSNumber numberWithInt:2],
                   [NSNumber numberWithInt:3],
                   [NSNumber numberWithInt:4],
                   nil];

NSArray *array2 = [NSArray arrayWithObjects:
                   [NSNumber numberWithInt:10],
                   [NSNumber numberWithInt:20],
                   [NSNumber numberWithInt:30],
                   [NSNumber numberWithInt:40],
                   nil];

NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:[array1 count]];

for (int i=0; i<[array1 count]; i++) {
    int result = [[array1 objectAtIndex:i] intValue] + [[array2 objectAtIndex:i] intValue];
    [resultArray addObject:[NSNumber numberWithInt:result]];                                
} 

Thanks, I appreciate any comments.

  • 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-05-28T20:40:47+00:00Added an answer on May 28, 2026 at 8:40 pm

    Here an example of doing this with blocks and concurrent enumeration:

    NSArray *array1 = [NSArray arrayWithObjects:
                       [NSNumber numberWithInt:1],
                       [NSNumber numberWithInt:2],
                       [NSNumber numberWithInt:3],
                       [NSNumber numberWithInt:4],
                       nil];
    
    NSArray *array2 = [NSArray arrayWithObjects:
                       [NSNumber numberWithInt:10],
                       [NSNumber numberWithInt:20],
                       [NSNumber numberWithInt:30],
                       [NSNumber numberWithInt:40],
                       nil];
    
    NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:[array1 count]];
    for (int i=0; i<[array1 count]; i++) {
        [resultArray addObject:[NSNull null]];
    }
    
    dispatch_queue_t resultArrayQueue = dispatch_queue_create("com.yourcompany.appname.resultsArrayQueue", DISPATCH_QUEUE_SERIAL);
    
    [array1 enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        int num1 = [(NSNumber *)obj intValue];
        int num2 = [[array2 objectAtIndex:idx] intValue];
        NSNumber *result = [NSNumber numberWithInt:(num1 + num2)];
        dispatch_async(resultArrayQueue, ^{
            [resultArray replaceObjectAtIndex:idx withObject:result];
        });
    }];
    
    NSLog(@"Result array: %@", resultArray);
    

    Whether it’s faster or not would require profiling to determine. My hunch is that it’s not much faster, and may even be slower because of the (relatively small, but still extant) overhead of GCD dispatches. However, for more complex computation than simple addition, this gives you an idea of how to use concurrent enumeration to easily do something with each element in an array in a multicore-aware way.

    If you had x number of arrays in a top-level array (as described in your comment), you could replace the -enumerateObjectsWithOptions:usingBlock: call with something like this:

    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_apply(numberOfItemsInEachArray, globalQueue, ^(size_t idx) {
        NSInteger sum = 0;
        for (NSArray *array in parentArray) {
            sum += [[array objectAtIndex:idx] integerValue];
        }
    
        NSNumber *result = [NSNumber numberWithInteger:sum];
        dispatch_async(resultArrayQueue, ^{
            [resultArray replaceObjectAtIndex:idx withObject:result];
        });
    });
    

    dispatch_apply() is a Grand Central Dispatch function that simply runs a block of code a specified number of times. Since we’re telling it to use a global concurrent queue that we got with dispatch_get_global_queue(), it can run different invocations of the block concurrently, providing a (possible) performance benefit on multicore machines. As with most programming problems, there are plenty of other ways to approach this problem. This is just one that came to mind for me.

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

Sidebar

Related Questions

I have two arrays of System.Data.DataRow objects which I want to compare. The rows
Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
I have an NSArray of UILocalNotification objects that I need to sort according to
I have an NSArray, and I want to split it into two equal pieces
I have two arrays, one that is made up of NSDates and one that's
I have two arrays, an NSMutableArray and an NSArray. The NSMutableArray is the store,
I have two NSArray s of Movie objects called DVD and VHS. I'd like
In my NSArray, I have two types of objects, let's say objects of class
I have these objects that are unique except for two columns once of which
I have two applications written in Java that communicate with each other using XML

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.