Swift Program to Check if two Dictionaries are equal


In Swift, a dictionary is an unordered collection in which data is stored in the form of key-value pairs, where keys are the unique identifiers. So to check the equality of two dictionaries we first need to check if the size of both the dictionaries is equal or not. If yes, then we check if both dictionaries contain the same key-value pairs or not. If both conditions are equal, then that means the given two dictionaries are equal. If any of the conditions are false then that means the given dictionaries are not equal.

Algorithm

  • Step 1 − Create a function which takes two arguments and returns a boolean value.

  • Step 2 − In the function, first we check if the count of both the given dictionaries is the same or not.

  • Step 3 − If the count is equal, then it checks whether the given key-value pairs are equal or not.

  • Step 4 − If the key-value pairs are equal, then it will return true, otherwise it will return false.

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

  • Step 6 − Now call the above-created function and pass them into it.

  • Step 7 − Display the result.

Example

In the following Swift program, we will check if the two dictionaries are equal. So create two dictionaries. Then create a function named checkEquality() which takes two dictionaries as an argument and returns a bool value which represents whether the given dictionaries are equal or not. So it first checks if the given dictionaries have the same number of key-value pairs. If not, then it will return false. If yes, then it will iterate through each key and value pairs in the first dictionary and checks if the second dictionary also contains the same key-value pairs or not. If any of the key-value pair in the two dictionaries does not match, then it will return false, else it will return true. So in the given example, both the dictionaries are the same so we get the “Both the dictionaries are equal” message in the output.

import Foundation
import Glibc

// Function to check if the given two dictionaries are equal or not
func checkEquality(D1: [String:String], D2: [String: String])-> Bool {

   // Checking if both the dictionaries have the same size or not
   guard D1.count == D2.count else {
      return false
   }
    
   // Checking if both the dictionaries have same key-value pairs or not
   for (key, value) in D1{
      guard let val = D2[key], value == val else {
         return false
      }
   }
   return true
}

let dict1 = ["name": "Mona", "company": "XMK.pvt", "city": "Mumbai"]
let dict2 = ["name": "Mona", "company": "XMK.pvt", "city": "Mumbai"]

if checkEquality(D1:dict1, D2: dict2) {
   print("Both the dictionaries are equal")
} else {
   print("Given dictionaries are not equal")
}

Output

Both the dictionaries are equal

Conclusion

So this is how we can check if two dictionaries are equal or not. The above-discussed method is the most efficient method to check the equality of two dictionaries. You can also use the == operator and isEqual(to:) method, but they are not as good as the discussed method. Also isEqual(to:) function only works for NSDictionary.

Updated on: 09-May-2023

406 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements