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:.
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:.
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.
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.
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:
- '
NSArrayorNSMutableArray, 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 anNSArray, 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 combinedNSArray. - Use
addObjectsFromArray:when appending into anNSMutableArray. - 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.

