How to merge two sorted 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.
Introduction
When both input arrays are already sorted, you should not sort everything again after concatenation. The efficient solution is a two-pointer merge, which walks both arrays once and produces a sorted result in linear time.
Use the standard two-pointer algorithm
The idea is simple: keep one index for each array, compare the current elements, append the smaller one, and advance only that index.
This runs in O(n + m) time because each element is inspected once. The extra space is also O(n + m) because the merged array stores every element from both inputs.
reserveCapacity is worth keeping. It avoids repeated reallocation while the result grows, which is a small but real performance improvement in Swift.
Make it generic for any comparable type
If you want the same logic for strings, dates, or custom comparable models, make the function generic:
This version works because Comparable guarantees the ordering operators needed for the merge. It is a good default when you want reusable utility code in a Swift project.
If you own the destination buffer
Some interview problems give you a first array with spare capacity and ask for an in-place merge. In that case, fill from the end so you do not overwrite values you still need:
This variant is still linear, but it avoids allocating a second result array because it reuses the storage you already have.
Why concatenating and sorting is usually worse
You might see code like this:
It is concise, and for tiny arrays it may be completely fine. The tradeoff is that it throws away the fact that both inputs are already sorted. Sorting the combined result costs more work than a merge, so the two-pointer version scales much better as the arrays grow.
In performance-sensitive code, use the structure you already know about the inputs instead of asking the sort algorithm to rediscover it.
Common Pitfalls
The most common bug is forgetting to append the remaining tail after one array runs out. That silently drops values.
Another mistake is assuming the inputs are sorted without checking the contract. If either array is unsorted, the merged result will also be wrong even though the code looks correct.
Off-by-one errors are also common in the in-place version, especially when one array is empty. Test edge cases such as two empty arrays, one empty array, duplicates, and arrays of different lengths.
Finally, do not optimize prematurely by replacing clear code with tricky index math unless you have measured a real problem. The standard merge is already efficient and easy to maintain.
Summary
- Use a two-pointer merge to combine two sorted arrays in linear time.
- Reserve result capacity in Swift to reduce reallocations.
- Prefer a generic
T: Comparableversion when the utility should work with more thanInt. - Use the back-to-front in-place technique only when you already have destination buffer space.
- Avoid concatenating and sorting unless the arrays are tiny and simplicity matters more than performance.
Related reading
- How to merge two sorted arrays into a sorted array?
- How to merge two sorted arrays into a sorted array?
- how to merge two sorted integer array in place using On time and O1 space cost
- How to minimize visual width of binary search tree?
- How to Navigate from one View Controller to another using Swift
- How to open a URL in Swift?
- How to migrate GIT repository from one server to a new one
- How to modify a pull request on GitHub to change target branch to merge into?

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.