
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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)
- Related Articles
- Swift Program to Find Minimum Key-Value Pair in the Dictionary
- Add key-value pair in C# Dictionary
- Add a key value pair to dictionary in Python
- Python program to find the second maximum value in Dictionary
- Get key with maximum value in Dictionary in Python
- Swift Program to Find the Size of Dictionary
- Python program to get maximum of each key Dictionary List
- C# Program to find a key in Dictionary
- Python Program to print key value pairs in a dictionary
- C++ Program to Update value of Dictionary using key
- Swift Program to sort the Dictionary
- Java Program to remove key value pair from HashMap?
- Appending a key value pair to an array of dictionary based on a condition in JavaScript?
- Swift Program to Check the dictionary is Empty
- Accessing Key-value in a Python Dictionary
