How do I concatenate or merge arrays in Swift?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Concatenating or merging arrays is a fundamental operation in programming, and Swift provides several intuitive and powerful ways to achieve this. This piece will explore various techniques to concatenate arrays in Swift, using examples for clarity.
Understanding Arrays in Swift
In Swift, arrays are collections that hold elements of the same type, stored in an ordered list. Arrays can grow and shrink in size dynamically. The type is specified as Array<Element> or simply [Element].
Array Concatenation using the + Operator
Swift overloads the + operator to allow for easy concatenation of two arrays. This operator returns a new array, which is a combination of the two sources.
Example:
Technical Explanation:
- The
+operator creates a new array. - The original arrays (
array1andarray2) remain unchanged. - This operation is O(n) where n is the total number of elements in the result.
Using the append(contentsOf:) Method
If you prefer to mutate one of the original arrays by adding elements from another, use append(contentsOf:).
Example:
Technical Explanation:
- The
append(contentsOf:)method mutatesoriginalArray. - Suitable for scenarios where it's acceptable or preferable to modify the existing array.
- Also O(n) in time complexity, where n is the number of elements being appended.
Merging Arrays with reduce(_:_:)
Another approach is to use the reduce(_:_:) method, which processes each element of a sequence using a closure.
Example:
Technical Explanation:
reduce([])uses an empty array as the initial result.- The closure
$0 + $1concatenates each array to the accumulative result. - This approach is valuable when merging an array of arrays.
Alternative: Flattening Nested Arrays
In cases where you have nested arrays, and wish to flatten them, flatMap(_:) is useful.
Example:
Technical Explanation:
flatMap(_:)passes each element of the sequence to the closure.- The result is then merged into a single-level array.
- It effectively concatenates all inner arrays.
Key Considerations
- Immutable vs Mutating: Decide whether you want to mutate existing arrays or work with immutable copies.
- Performance: For large datasets, consider the cost of copying elements and choose the most efficient method for your needs.
- Type Safety: All arrays involved must hold the same element type.
Summary Table
| Method | Mutability | Performance | Applicable Scenarios |
+ Operator | Immutable | O(n) | Simple concatenation of two arrays |
append(contentsOf:) Method | Mutable | O(n) | Directly mutate one array |
reduce(_:_:) | Immutable | O(n) with overhead | Merging multiple arrays into one |
flatMap(_:) | Immutable | O(n) | Flattening nested arrays into a single array |
By understanding these methods, you can efficiently merge or concatenate arrays in Swift to construct the data structures necessary for your applications. By leveraging Swift's concise syntax and array operations, you can achieve powerful manipulations with relative ease.
Related reading
- How do I concatenate two arrays in C?
- How do I concatenate two lists in Python?
- How do I convert a byte array to Base64 in Java?
- How do I convert a Java 8 IntStream to a List?
- How do I concatenate strings in Swift?
- How do I configure full URLs in xcconfig files
- How do I configure AWS CodeBuild to build only when push to master branch is made?
- How do I configure git to ignore some files locally?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.