0

I'm learning Swift and can't find the solution for my problem...

I have two Dictionaries and want to combine them:

dict1 = ["A": 1, "B": 2, "D": 5]
dict2 = ["A": 3, "C": 9, "D": 4]

The outcome should be a new Dictionary like:

dict3 = ["A": 4, "B": 2, "C": 9, "D": 9]
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • It's [Swift](https://en.wikipedia.org/wiki/Swift_(programming_language)), not [SWIFT](https://en.wikipedia.org/wiki/Society_for_Worldwide_Interbank_Financial_Telecommunication) – Alexander Feb 19 '22 at 14:40

1 Answers1

7

You can use Dictionary merging method and pass the plus sign (addition operator) as the uniquingKeysWith parameter:

let dict3 = dict1.merging(dict2, uniquingKeysWith: +)  // ["A": 4, "B": 2, "D": 9, "C": 9]
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571