
- 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 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)
- Related Articles
- Swift Program to Find Maximum Key-Value Pair in the Dictionary
- Add key-value pair in C# Dictionary
- Add a key value pair to dictionary in Python
- Swift Program to Find the Size of Dictionary
- 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
- Python program to find the second maximum value in Dictionary
- Minimum XOR Value Pair in C++
- Accessing Key-value in a Python Dictionary
- Swift Program to Find Minimum Set Element Using Library Function
