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 Remove All the Elements from the Array
This tutorial will discuss how to write swift program to get the magnitude of the number.
An array is an ordered collection which is used to store same type of data. For example, if any array is of integer type then it will only store integers, you are strictly not allowed to store elements of other data types like string, float, etc.
To remove all the elements from the specified array Swift provide an in-built library function named removeAll(). The removeAll() function will delete all the items from the specified array. Or it can delete elements that match the given condition.
Below is a demonstration of the same ?
Syntax
Following is the syntax ?
arrayName.removeAll(where:)
Here, where is the condition. When the condition is satisfied, then the elements are removed. It is optional.
Algorithm
Following is the algorithm ?
Step 1 ? Create an array with values.
Step 2 ? Remove all the elements using removeAll() function ?
arrNumbers.removeAll()
Step 3 ? Print the output
Remove all the elements from the array
Example
The following program shows how to remove all the elements from the array.
<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 an array of integer type</span> <span class="token keyword">var</span> arrNumbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">44</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">34</span><span class="token punctuation">]</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Original Array:"</span><span class="token punctuation">,</span> arrNumbers<span class="token punctuation">)</span> <span class="token comment">// Remove all the elements from the array</span> arrNumbers<span class="token punctuation">.</span><span class="token function">removeAll</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">"Modified Array:"</span><span class="token punctuation">,</span> arrNumbers<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Original Array: [44, 100, 10, 19, 29, 200, 300, 34] Modified Array: []
Here, in the above code, we have an array of integer type named arrNumbers. Now we remove all the elements of arrNumbers using removeAll() function ?
arrNumbers.removeAll()
Hence the resultant array is empty.
Remove all the elements that match the condition
Example
The following program shows how to remove all the elements from the array that matches the given condition.
<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 an array of string type</span>
<span class="token keyword">var</span> arrNames <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"Tom"</span><span class="token punctuation">,</span> <span class="token string">"Pinky"</span><span class="token punctuation">,</span> <span class="token string">"Owl"</span><span class="token punctuation">,</span> <span class="token string">"Pompom"</span><span class="token punctuation">,</span> <span class="token string">"XOXO"</span><span class="token punctuation">,</span> <span class="token string">"Jerry"</span><span class="token punctuation">,</span> <span class="token string">"Mom"</span><span class="token punctuation">]</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Original Array:"</span><span class="token punctuation">,</span> arrNames<span class="token punctuation">)</span>
<span class="token comment">// Remove all the elements from the array</span>
<span class="token comment">// Whose length is less than 4</span>
arrNames<span class="token punctuation">.</span><span class="token function">removeAll</span><span class="token punctuation">(</span>where<span class="token operator">:</span> <span class="token punctuation">{</span>$<span class="token number">0.</span>count <span class="token operator"><</span> <span class="token number">4</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">"Modified Array:"</span><span class="token punctuation">,</span> arrNames<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Original Array: ["Tom", "Pinky", "Owl", "Pompom", "XOXO", "Jerry", "Mom"] Modified Array: ["Pinky", "Pompom", "XOXO", "Jerry"]
Here, in the above code, we have an array named arrNames of string type. Now we remove all the elements whose size is less than 4. So to this we passed this condition in the removeAll() function ?
arrNames.removeAll(where: {$0.count < 4})
Here $0 represents the first element whose length is less than 4. So the removed elements are: ["Tom", "Owl", "Mom"]. Therefore the resultant array is ["Pinky", "Pompom", "XOXO", "Jerry"].
