Swift Program to Find Minimum Key-Value Pair in the Dictionary


This tutorial will discuss how to write swift program to find minimum key-value pair in the dictionary.

A dictionary is used to store data in the form of key-value pair in the collation without any defined-order. Here the type of the keys are same as well as the type of the values are also same. Each key is behave like an identifier for the associate value inside the dictionary and each value has a unique key. Swift dictionary is just like the real dictionary. It loop up values according to their identifier.

Syntax

Following is the syntax for creating dictionary −

Var mydict = [KeyType: ValueType]()
Or
Var mydict : [KeyType:ValueType] = [key1:value1, key2:value2, key3:value3]

To find the minimum key-value pair, we use min() function. This function will return minimum key-value pair in the dictionary. Here the returned object of type Optional<T>. So we type cast the value into the required datatype. If the given dictionary is empty, then it will return nil.

Below is a demonstration of the same −

Input

Suppose our given input is −

MyDict = ["installment1": 2000, “installment2": 3902, "installment3": 7832, "installment4": 8743]

Output

The desired output would be −

Minimum key-value pair - "installment1": 2000

Syntax

Following is the syntax −

Dict.min(by: {operator})!

Here, operator is a closer which is used to accept condition and return a bool value.

Algorithm

Following is the algorithm −

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

  • Step 2 − Find the minimum key or value using min() function and wrap the output value into the required data type using ! operator.

For minimum key:
let minimumYear = myinstalments.min(by:{$0.key < $1.key})
For minimum value:
let minimumInstalment = myinstalments.min(by:{$0.value < $1.value})
  • Step 3 − Print the output

Finding minimum key in the dictionary

Example

The following program shows how to find minimum key along with value in the dictionary.

import Foundation import Glibc // Creating a dictionary var myinstalments : [String:Int] = ["year1": 23000, "year2": 24000, "year3": 19000, "year4": 19902] // Minimum key let minimumYear = myinstalments.min(by:{$0.key < $1.key}) print("Yearly instalments: ", myinstalments) print("Minimum year:", minimumYear!)

Output

Yearly instalments: ["year3": 19000, "year2": 24000, "year1": 23000, "year4": 19902]
Minimum year: (key: "year1", value: 23000)

Here, in the above code, we have a dictionary which contain the yearly instalments. Now we find the minimum year using min() function −

let minimumYear = myinstalments.min(by:{$0.key < $1.key})

Here, $0.key < $1.key means the first key should be less than the second key. And $0 and $1 represent the first and second arguments

Hence the resultant key value pair is (key: "year1", value: 23000).

Finding minimum value in the dictionary

Example

The following program shows how to find minimum value along with key in the dictionary.

import Foundation import Glibc // Creating a dictionary var myinstalments : [String:Int] = ["year1": 23000, "year2": 24000, "year3": 19000, "year4": 19902] // Minimum value let minimumInstalment = myinstalments.min(by:{$0.value < $1.value}) print("Yearly instalments: ", myinstalments) print("Minimum instalment:", minimumInstalment!)

Output

Yearly instalments: ["year1": 23000, "year2": 24000, "year3": 19000, "year4": 19902]
Minimum instalment: (key: "year3", value: 19000)

Here, in the above code, we have a dictionary which contain the yearly instalments. Now we find the minimum amount using min() function −

let minimumInstalment = myinstalments.min(by:{$0.value < $1.value})

Here, $0.value < $1.value means the first value should be less than the second value. And $0 and $1 represent the first and second arguments

Hence the resultant key value pair is (key: "year3", value: 19000)

Updated on: 20-Oct-2022

575 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements