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 Get the Size of Set
This tutorial will discuss how to write swift program to get the size of set.
Set is a primary collection type in Swift. It is an unordered collection which stores unique values of same data type. You are not allowed to store different type of values in the same set. A set can be mutable or immutable.
To get the size of a Set Swift provide an in-built property named count. It will return the total number of elements present in the specified count.
Below is a demonstration of the same ?
Input
Suppose our given input is ?
MySet = [2, 45, 67, 12, 33, 55]
Output
The desired output would be ?
Size = 6
Syntax
Following is the syntax ?
setName.count
Algorithm
Following is the algorithm ?
Step 1 ? Declare and initialise a Set with value.
Step 2 ? Find the size of the set using count property ?
var setSize = myNum.count
Step 3 ? Print the output
Example 1
The following program shows how to calculate the size of a set.
<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 Set of integer Type</span> <span class="token keyword">var</span> myNum<span class="token operator">:</span> Set <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">56</span><span class="token punctuation">,</span> <span class="token number">78</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">45</span><span class="token punctuation">]</span> <span class="token comment">// Finding the size of the Set</span> <span class="token comment">// using count property</span> <span class="token keyword">var</span> setSize <span class="token operator">=</span> myNum<span class="token punctuation">.</span>count <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Set: "</span><span class="token punctuation">,</span> myNum<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Size of the set is: "</span><span class="token punctuation">,</span> setSize<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Set: [45, 12, 23, 56, 9, 78] Size of the set is: 6
Here, in the above code, we have a set of integer type [45, 12, 23, 56, 9, 78]. Now we find the size of the using count property ?
var setSize = myNum.count
Hence the size of the set is 6.
Example 2
The following program shows how to calculate the size of a set.
<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 Set of string Type</span> <span class="token keyword">var</span> myNames<span class="token operator">:</span> Set <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"Susma"</span><span class="token punctuation">,</span> <span class="token string">"Punita"</span><span class="token punctuation">,</span> <span class="token string">"Piku"</span><span class="token punctuation">,</span> <span class="token string">"Poonam"</span><span class="token punctuation">,</span> <span class="token string">"Soona"</span><span class="token punctuation">]</span> <span class="token comment">// Creating an empty Set</span> <span class="token keyword">var</span> mySet <span class="token operator">=</span> Set<span class="token operator"><</span>String<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// Finding the size of the Set</span> <span class="token comment">// using count property</span> <span class="token keyword">var</span> setSize1 <span class="token operator">=</span> myNames<span class="token punctuation">.</span>count <span class="token keyword">var</span> setSize2 <span class="token operator">=</span> mySet<span class="token punctuation">.</span>count <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Set 1: "</span><span class="token punctuation">,</span> myNames<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Size of the set is: "</span><span class="token punctuation">,</span> setSize1<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Set 2: "</span><span class="token punctuation">,</span> mySet<span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Size of the set is: "</span><span class="token punctuation">,</span> setSize2<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Set 1: ["Piku", "Susma", "Punita", "Soona", "Poonam"] Size of the set is: 5 Set 2: [] Size of the set is: 0
Here, in the above code, we have two sets: myNames is of String type ["Piku", "Punita", "Poonam", "Soona", "Susma"] and mySet is an empty set. Now we find the size of the given sets using count property ?
var setSize1 = myNames.count var setSize2 = mySet.count
Hence the size of myNames is 5 and the size of setSize2 is 0.
