Swift Program to Get key from Dictionary using the value


A dictionary is a collection of key-value pairs. Which means keys and values are interrelated with each other. So using a value we can easily get single or multiple keys from the given dictionary.

Algorithm

  • Step 1 − Create a dictionary with key-value pairs.

  • Step 2 − Then create a function which will return the key from the dictionary using the value.

  • Step 3 − Inside the function we run a for-in loop which iterates over the key-value pairs of the given dictionary.

  • Step 4 − Now check if the current value is equal to the specified value. If yes, then return the related key. Otherwise, return 0 or nil.

  • Step 5 − If a value has multiple keys, then store them in an array and then return that array.

  • Step 6 − Create a variable to store the value.

  • Step 7 − Call the function and pass the value and the dictionary as a parameter in it.

  • Step 8 − Display the output.

Example 1

In the following Swift program, we are going to get a key from the dictionary using the value. So for that, we are going to create a dictionary and then create a function which will return a key from the given dictionary according to the specified value.

import Foundation
import Glibc
 
let myLang = [2001:"C#", 2002:"Swift", 2003:"C++", 2004: "Java", 2005: "Python"]

func findKey(mvalue: String, dict: [Int:String])-> Int {

   for (key, value) in dict {
      if value == mvalue {
         return key 
      }
   }
   return 0
}

let val = "Java"

let resultantKey = findKey(mvalue: val, dict: myLang)
 
if (resultantKey == 0) {
   print("Key not found. Please try Again")
} else {
   print("The key for the value:\(val) is \(resultantKey)")
}

Output

The key for the value:Java is 2004

Example 2

In the following Swift program, we are going to get the key from the dictionary using the value. So for that, we will create a dictionary and then a function which will return all the keys that have the specified values because as we know that a dictionary can only have unique keys, but can have duplicate values. So a single value can have multiple keys.

import Foundation
import Glibc
 
let myLang = ["C#": 10, "Swift": 30, "C++": 30, "Java": 32, "Python": 30, "C": 10]

func findKey(mvalue: Int, dict: [String:Int])-> [String] {

   var resKeys:[String] = [] 

   for (key, value) in dict {
      if value == mvalue  {
         resKeys.append(key) 
      }
   }
   return resKeys
}

let val = 30

let resultantKey = findKey(mvalue: val, dict: myLang)
 
if resultantKey.isEmpty {
   print("Key not found. Please try Again")
} else {
   print("The keys for the value:\(val) are \(resultantKey)")
}

Output

The keys for the value:30 are ["C++", "Swift", "Python"]

Conclusion

So this is how we can get a key from a dictionary using the value. The working of both methods is almost the same, the first one works for unique key-value pairs and the other works for those unique keys that have duplicate values. Because as we know that in a dictionary keys are unique but values can be duplicated.

Updated on: 09-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements