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.

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

<span class="token comment">// Creating a dictionary</span>
<span class="token keyword">var</span> myinstalments <span class="token operator">:</span> <span class="token punctuation">[</span>String<span class="token operator">:</span>Int<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"year1"</span><span class="token operator">:</span> <span class="token number">23000</span><span class="token punctuation">,</span> <span class="token string">"year2"</span><span class="token operator">:</span> <span class="token number">24000</span><span class="token punctuation">,</span> <span class="token string">"year3"</span><span class="token operator">:</span> <span class="token number">19000</span><span class="token punctuation">,</span> <span class="token string">"year4"</span><span class="token operator">:</span> <span class="token number">19902</span><span class="token punctuation">]</span>

<span class="token comment">// Minimum key</span>
<span class="token keyword">let</span> minimumYear <span class="token operator">=</span> myinstalments<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>by<span class="token operator">:</span><span class="token punctuation">{</span>$<span class="token number">0.</span>key <span class="token operator"><</span> $<span class="token number">1.</span>key<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Yearly instalments: "</span><span class="token punctuation">,</span> myinstalments<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Minimum year:"</span><span class="token punctuation">,</span> minimumYear<span class="token operator">!</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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.

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

<span class="token comment">// Creating a dictionary</span>
<span class="token keyword">var</span> myinstalments <span class="token operator">:</span> <span class="token punctuation">[</span>String<span class="token operator">:</span>Int<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"year1"</span><span class="token operator">:</span> <span class="token number">23000</span><span class="token punctuation">,</span> <span class="token string">"year2"</span><span class="token operator">:</span> <span class="token number">24000</span><span class="token punctuation">,</span> <span class="token string">"year3"</span><span class="token operator">:</span> <span class="token number">19000</span><span class="token punctuation">,</span> <span class="token string">"year4"</span><span class="token operator">:</span> <span class="token number">19902</span><span class="token punctuation">]</span>

<span class="token comment">// Minimum value</span>
<span class="token keyword">let</span> minimumInstalment <span class="token operator">=</span> myinstalments<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>by<span class="token operator">:</span><span class="token punctuation">{</span>$<span class="token number">0.</span>value <span class="token operator"><</span> $<span class="token number">1.</span>value<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Yearly instalments: "</span><span class="token punctuation">,</span> myinstalments<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Minimum instalment:"</span><span class="token punctuation">,</span> minimumInstalment<span class="token operator">!</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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: 2022-10-20T08:25:11+05:30

963 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements