Objective-C
arrays
array manipulation
programming
code tutorial

How would I combine two arrays in Objective-C?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Combining two arrays in Objective-C is straightforward, but the best method depends on whether you want a new immutable array or you want to mutate an existing one. Foundation gives you both patterns, and choosing the right one keeps the code readable and avoids unnecessary copying. The key distinction is NSArray versus NSMutableArray.

Create a New Immutable Array

If you already have two NSArray instances and want a third combined result, use arrayByAddingObjectsFromArray:.

objective-c
1NSArray *first = @[@"A", @"B"];
2NSArray *second = @[@"C", @"D"];
3NSArray *combined = [first arrayByAddingObjectsFromArray:second];
4
5NSLog(@"%@", combined);

This returns a new array containing the elements from first followed by the elements from second.

Use this when:

  • the original arrays should remain unchanged
  • you want a concise immutable result
  • a one-off combined array is enough

Append Into a Mutable Array

If you want to build up an array progressively, use NSMutableArray and addObjectsFromArray:.

objective-c
1NSMutableArray *combined = [NSMutableArray arrayWithArray:@[@"A", @"B"]];
2NSArray *second = @[@"C", @"D"];
3
4[combined addObjectsFromArray:second];
5NSLog(@"%@", combined);

This mutates combined in place.

It is the better choice when:

  • you are aggregating data in multiple steps
  • performance matters and repeated immutable copying would be wasteful
  • you plan to remove or add more items later

Understand the Order of Elements

Both methods preserve order:

  • all elements from the first array appear first
  • all elements from the second array are appended after them

If order matters for later logic, this behavior is predictable and simple.

Combining Arrays of Objects

Objective-C arrays store objects, not primitive C values directly. So if you want integers, wrap them in NSNumber.

objective-c
1NSArray *numbers1 = @[@1, @2, @3];
2NSArray *numbers2 = @[@4, @5];
3NSArray *combined = [numbers1 arrayByAddingObjectsFromArray:numbers2];
4
5NSLog(@"%@", combined);

This works because the @1 syntax creates NSNumber objects.

If You Need Unique Values Only

A straight combine operation does not remove duplicates. If uniqueness matters, one easy approach is to merge first and then pass through an ordered set.

objective-c
1NSArray *first = @[@"A", @"B", @"B"];
2NSArray *second = @[@"B", @"C"];
3NSArray *combined = [first arrayByAddingObjectsFromArray:second];
4NSArray *unique = [[NSOrderedSet orderedSetWithArray:combined] array];
5
6NSLog(@"%@", unique);

This preserves the first appearance order while removing duplicates.

When a C Array Is Actually Involved

Sometimes people say "array" but they really mean a C array such as int values[]. That is a different problem. NSArray and NSMutableArray are Objective-C collection classes, while C arrays need manual allocation or copying.

So first clarify whether you are dealing with:

  • 'NSArray or NSMutableArray, or'
  • a raw C array

The API above applies only to Foundation collection objects.

A Practical Memory Note

arrayByAddingObjectsFromArray: creates a new array, which is perfectly fine for ordinary app code. But if you are combining large arrays repeatedly in a loop, copying into a mutable array and appending may be more efficient.

That is not an optimization to worry about immediately, but it is the right mental model.

Common Pitfalls

  • Calling addObjectsFromArray: on an NSArray, which is immutable.
  • Forgetting that Objective-C arrays store objects, not raw primitive values.
  • Expecting the combine operation to remove duplicates automatically.
  • Mixing up Foundation arrays with C arrays and looking for the wrong API.
  • Repeatedly creating new immutable combined arrays in a hot loop when a mutable array would be simpler.

Summary

  • Use arrayByAddingObjectsFromArray: to create a new combined NSArray.
  • Use addObjectsFromArray: when appending into an NSMutableArray.
  • Objective-C arrays preserve order when combined.
  • Duplicates are preserved unless you remove them explicitly.
  • Make sure you know whether you are working with Foundation arrays or raw C arrays before choosing the API.

Course illustration
Course illustration

All Rights Reserved.