Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Swift Program to Find the Size of Dictionary
This tutorial will discuss how to write swift program to find the size of 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 size of the dictionary Swift provide a property named count. It will count all the key-value pair in the dictionary and return their total count.
Below is a demonstration of the same ?
Input
Suppose our given input is ?
Mydict = [1: "owl", 2: "kiwi", 3: "frog"] Size = 3
Syntax
Following is the syntax ?
dictName.count
Algorithm
Following is the algorithm ?
Step 1 ? Creating dictionaries with key-value pair.
Step 2 ? Calculating the size of dictionary using count property.
var size1 = mydict1.count var size2 = mydict2.count
Step 3 ? Print the output
Example
The following program shows how to count the size of 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 dictionaries</span> <span class="token keyword">var</span> mydict1 <span class="token operator">:</span> <span class="token punctuation">[</span>Int<span class="token operator">:</span>Int<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token operator">:</span><span class="token number">34</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token operator">:</span><span class="token number">56</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token operator">:</span><span class="token number">98</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token operator">:</span><span class="token number">3803</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token operator">:</span><span class="token number">23</span><span class="token punctuation">]</span> <span class="token keyword">var</span> mydict2<span class="token operator">:</span> <span class="token punctuation">[</span>Int<span class="token operator">:</span>String<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token operator">:</span><span class="token string">"peacock"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token operator">:</span><span class="token string">"bird"</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token operator">:</span><span class="token string">"rat"</span><span class="token punctuation">]</span> <span class="token comment">// Calculating the size of dictionary</span> <span class="token keyword">var</span> size1 <span class="token operator">=</span> mydict1<span class="token punctuation">.</span>count <span class="token keyword">var</span> size2 <span class="token operator">=</span> mydict2<span class="token punctuation">.</span>count <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Dictionary:"</span><span class="token punctuation">,</span> mydict1<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Size:"</span><span class="token punctuation">,</span> size1<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Dictionary:"</span><span class="token punctuation">,</span> mydict2<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Size:"</span><span class="token punctuation">,</span> size2<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Dictionary: [5: 3803, 8: 98, 6: 23, 4: 56, 2: 34] Size: 5 Dictionary: [2: "bird", 1: "peacock", 3: "rat"] Size: 3
Here, in the above code, we have two dictionaries: mydict1 and mydict2. Now we find their size using count property.
var size1 = mydict1.count var size2 = mydict2.count
So the output is ?
Dictionary: [5: 3803, 8: 98, 6: 23, 4: 56, 2: 34] Size: 5 Dictionary: [2: "bird", 1: "peacock", 3: "rat"] Size: 3
