Swift Program to Merge two Dictionaries


To merge the content of two dictionaries Swift provides a pre-defined function named merging(_:uniqueKeysWith:). This function creates a new dictionary by merging the elements of two dictionaries. It also uses a combining closure to check the values for duplicate keys.

Syntax

func merging(otherDict){comClosure} 

This function takes two parameters and they are −

  • OtherDict − It is a dictionary to merge.

  • comClosure − It is a closure which takes current and new values for the duplicate keys and then returns the desired value for the output dictionary. It is an optional parameter that means if you don’t want to customise the merge function you can remove this parameter.

Algorithm

  • Step 1 − Create two dictionaries with key-value pairs.

  • Step 2 − Print the original dictionaries.

  • Step 3 − Now merge the elements of the given dictionaries using the merging() function and store the result in a new dictionary.

  • Step 4 − Print the resultant dictionary.

Example 1

In the following Swift program, we are going to merge two dictionaries. So first we create two dictionaries of the same data types. Then we will merge them using the merging() function. In this function, we pass two parameters one is the second dictionary and a closure. The closure has two arguments “_” and “new”, where “_” represents wildcard which means the value already in nDict for any keys that exist in both the dictionaries and we simply ignore it in the closure and the new parameter represents the value from “mDict” for any key exists in both dictionaries and we return it to overwrite the value in nDict. Finally print the resulting dictionary.

import Foundation
import Glibc

let nDict = ["x": 234, "y": 221, "z": 922]
let mDict = ["p": 12, "q": 182, "r": 2781]

print("Original Dictionaries:")
print("Dictionary 1: ", nDict)
print("Dictionary 2: ", mDict)

// Merging two dictionaries
let mergeDict = nDict.merging(mDict) { (_, new) in new }

print("\nMerged Dictionary:", mergeDict)

Output

Original Dictionaries:
Dictionary 1:  ["x": 234, "z": 922, "y": 221]
Dictionary 2:  ["p": 12, "q": 182, "r": 2781]

Merged Dictionary: ["z": 922, "y": 221, "p": 12, "x": 234, "q": 182, "r": 2781]

Example 2

In the following Swift program, we are going to merge two dictionaries. So we will create two dictionaries of the same data types. Then we merged them using the merging() function. Here both dictionaries have some duplicate keys. So in the merging() function, we pass two parameters mDict and closure. In the closure, we pass two arguments current and new and it will return new that means for any keys that exist both in nDict and mDict, their values from mDict will overwrite the value in nDict. Finally display the resultant dictionary.

import Foundation
import Glibc

let nDict = [1: "dd", 2: "pp", 3: "ww"]
let mDict = [1: "oo", 8: "ee", 2: "tt"]

print("Original Dictionaries:")
print("Dictionary 1: ", nDict)
print("Dictionary 2: ", mDict)

// Merging two dictionaries
let mergeDict = nDict.merging(mDict) { (current, new) in new }

print("\nMerged Dictionary:", mergeDict)

Output

Original Dictionaries:
Dictionary 1:  [1: "dd", 2: "pp", 3: "ww"]
Dictionary 2:  [2: "tt", 1: "oo", 8: "ee"]
Merged Dictionary: [8: "ee", 3: "ww", 2: "tt", 1: "oo"]

Conclusion

So this is how we can merge two dictionaries using the merging() function. This function takes two parameters and returns a new dictionary which contains the key-value pairs from both the given dictionaries. It works well for dictionaries that contain unique key-value pairs and for dictionaries that contain some duplicate keys.

Updated on: 09-May-2023

752 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements