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 Check the dictionary is Empty
This tutorial will discuss how to write swift program to check the dictionary is empty.
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 check the given dictionary is empty or not we can use any of the following method ?
Using count property
Using isEmpty property
Method 1- Using count property
To check the given dictionary is empty or not, we check the size of the dictionary. If the size of the dictionary is 0 then the dictionary is empty otherwise not. So to find the size of the dictionary we use count property.
Below is a demonstration of the same ?
Input
Suppose our given input is ?
Mydict = [1: "Mona", 2: "Noh", 3: "Tarzen"] NO! The dictionary is not empty Here Mydict dictionary is not empty because the size of the Mydict is 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 ? If the size is equal to 0, then the dictionary is empty. Otherwise not.
Step 4 ? 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 operator">:</span><span class="token punctuation">]</span>
<span class="token comment">// Calculating the size of dictionaries</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 comment">// Checking if the given dictionaries are empty or not</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>size1 <span class="token operator">==</span> <span class="token number">0</span><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">"mydict1 is empty"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span><span class="token punctuation">{</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"mydict1 is not empty"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>size2 <span class="token operator">==</span> <span class="token number">0</span><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">"mydict2 is empty"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span><span class="token punctuation">{</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"mydict2 is not empty"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
mydict1 is not empty mydict2 is empty
Here, in the above code, we have two dictionaries: mydict1 and mydict2. Now we check they are empty or not using count property. So first we find their size using count property.
var size1 = mydict1.count var size2 = mydict2.count
Now we check they are empty or not ?
if (size1 == 0) // Condition is false{
print("mydict1 is not empty")
}
if (size2 == 0) // Condition is true{
print("mydict2 is empty")
}
Method 2- Using isEmpty property
To check the given dictionary is empty or not, we use isEmpty property. This property will return true, if the given dictionary is empty. Otherwise, it will return false.
Below is a demonstration of the same ?
Input
Suppose our given input is ?
Mydict = [] YES! The dictionary is not empty
Here, Mydict dictionary is empty so isEmpty will return true.
Syntax
Following is the syntax ?
dictName.isEmpty
Algorithm
Following is the algorithm ?
Step 1 ? Creating dictionaries with key-value pair.
Step 2 ? Check if the given dictionary is empty or not using isEmpty property.
mydict1.isEmpty
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>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">"CAR"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token operator">:</span> <span class="token string">"BUS"</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token operator">:</span><span class="token string">"BIKE"</span><span class="token punctuation">]</span>
<span class="token keyword">var</span> mydict2<span class="token operator">:</span> <span class="token punctuation">[</span>String<span class="token operator">:</span>String<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token operator">:</span><span class="token punctuation">]</span>
<span class="token comment">// Checking if the given dictionaries are empty or not</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>mydict1<span class="token punctuation">.</span>isEmpty <span class="token operator">==</span> <span class="token boolean">true</span><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">"mydict1 is empty"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span><span class="token punctuation">{</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"mydict1 is not empty"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>mydict2<span class="token punctuation">.</span>isEmpty <span class="token operator">==</span> <span class="token boolean">true</span><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">"mydict2 is empty"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span><span class="token punctuation">{</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"mydict2 is not empty"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
mydict1 is not empty mydict2 is empty
Here, in the above code, we have two dictionaries: mydict1 and mydict2. Now we check they are empty or not using isEmpty property.
if (mydict1.isEmpty == true) // Condition is false{
print("mydict1 is empty")
}
if (mydict2.isEmpty == true) // Condition is true{
print("mydict2 is empty")
}
else{
print("mydict2 is not empty")
}
