How can I combine two Dictionary instances 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
In Swift, combining two dictionaries is straightforward once you decide what should happen when both dictionaries contain the same key. Swift gives you both mutating and non-mutating APIs for this, and the conflict-resolution closure is the part that determines the real behavior.
Use merge to Modify an Existing Dictionary
If you want to update one dictionary in place, use merge.
This keeps the value from override whenever the same key appears in both dictionaries.
The conflict closure matters only when keys collide. If a key exists in just one dictionary, it is copied directly.
Use merging to Create a New Dictionary
If you want to keep both originals unchanged, use merging.
This is the better choice when you want an immutable result or you are composing values in a functional style.
Choosing Which Value Wins
The merge closure defines the rule for duplicate keys. The two most common policies are:
- keep the new value
- keep the existing value
Keep the new value:
Keep the existing value:
That makes the API flexible enough to express configuration layering, user overrides, caching, and other common patterns.
Merging Numeric or Aggregate Values
The closure does not have to choose one side unchanged. It can combine values however you need.
This is useful when dictionaries represent counts, totals, or metrics rather than simple replacement values.
Combining More Than Two Dictionaries
If you need to merge several dictionaries, reduce(into:) scales better than chaining many calls by hand.
This pattern is clean when building configuration from defaults, environment overrides, and user overrides.
Think About Semantics, Not Just Syntax
The technical merge is easy. The real design question is what duplicate keys mean in your domain.
Examples:
- configuration layering usually keeps the new value
- cached values may keep the existing value
- metrics or counters may sum the values
Choosing the closure deliberately is more important than memorizing whether the method name ends in -ing.
Performance and Type Notes
Swift dictionaries are value types, but they use copy-on-write semantics. That means merging does not immediately duplicate everything just because you assign the result to a new variable. It remains efficient in typical use.
Still, if you already own a mutable dictionary and want to update it, merge communicates the intent more directly and avoids creating a separate result variable.
Common Pitfalls
The biggest mistake is forgetting that duplicate keys require a conflict-resolution closure. Swift makes you choose because there is no universally correct default.
Another mistake is using merge when the original dictionary should stay unchanged. In that case, merging is the clearer API.
Developers also sometimes assume dictionaries preserve insertion order as a design guarantee they can build logic around. Dictionary ordering is not the right abstraction for configuration semantics.
Finally, think about value type compatibility. Both dictionaries must have the same key and value types unless you transform them before merging.
Summary
- Use
mergeto update an existing dictionary in place. - Use
mergingto create a new combined dictionary without mutating the originals. - The conflict-resolution closure decides what happens for duplicate keys.
- That closure can keep the old value, keep the new value, or combine both values.
- For several dictionaries,
reduce(into:)plusmergeis a clean scalable pattern.
Related reading
- How can I combine two HashMap objects containing the same types?
- How can I compare two lists in python and return matches
- How can I compare two sets of 1000 numbers against each other?
- How can I concatenate two arrays in C?
- How can I concatenate NSAttributedStrings?
- How can I connect to Android with ADB over TCP?
- How can I concatenate two arrays in Java?
- How can I configure the heap size when starting a Spring Boot application with embedded Tomcat?

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.