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


This tutorial will discuss how to write swift program to find maximum 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 maximum key-value pair, we use max() function. This function will return maximum key-value pair in the dictionary. 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 −

Maximum key-value pair: "installment4": 8743

Syntax

Following is the syntax −

Dict.max(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 maximum key or value using max() function and wrap the output value into the required data type using ! operator.

For maximum key:
let maximumYear = myinstalments.max(by:{$0.key < $1.key})
For maximum value:
let maximumInstalment = myinstalments.max(by:{$0.value < $1.value})
  • Step 3 − Print the output

Finding maximum key in the dictionary

Example

The following program shows how to find maximum 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] // Maximum key let maximumYear = myinstalments.max(by:{$0.key < $1.key}) print("Yearly instalments: ", myinstalments) print("Maximum year:", maximumYear!)

Output

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

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

let maximumYear = myinstalments.max(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: "year4", value: 19902)

Finding maximum value in the dictionary

Example

The following program shows how to find maximum 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] // Maximum value let maximumInstalment = myinstalments.max(by:{$0.value < $1.value}) print("Yearly instalments: ", myinstalments) print("Maximum Instalment:", maximumInstalment!)

Output

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

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

let maximumInstalment = myinstalments.max(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: "year2", value: 24000)

Updated on: 20-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements