Swift Program to Remove an Element from the Set

This tutorial will discuss how to write swift program to remove an element from the 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 remove an element from the set Swift provide an in-built library function named remove(). The remove() function delete the given element from the specified set. If the given element is not the part of the specified set, then it will return nil.

Below is a demonstration of the same ?

Input

Suppose our given input is ?

MySet = [23, 45, 67, 8, 90, 10, 100]
Remove = 45

Output

The desired output would be ?

Modified Set = [23, 67, 8, 90, 10, 100]

Syntax

Following is the syntax ?

setName.remove()

Algorithm

Following is the algorithm ?

  • Step 1? Create a set with values.

  • Step 2? Remove an element from the set using remove() function.

  • Step 3? Print the output

Example 1

The following program shows how to remove an element from the 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> setNumbers <span class="token operator">:</span> Set <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 Set:"</span><span class="token punctuation">,</span> setNumbers<span class="token punctuation">)</span>

<span class="token comment">// Remove element from the set</span>
setNumbers<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Modified Set:"</span><span class="token punctuation">,</span> setNumbers<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Original Set: [100, 34, 29, 300, 200, 44, 10, 19]
Modified Set: [100, 34, 29, 300, 200, 44, 19]

Here, in the above code, we have a set of integer type named setNumbers. Now we remove an element that is 10 from the set using remove () function ?

setNumbers.remove(10)

Hence after removing element the resultant set is [100, 34, 29, 300, 200, 44, 19]

Example 2

The following program shows how to remove an element from the 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> setNames <span class="token operator">:</span> Set <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"Hen"</span><span class="token punctuation">,</span> <span class="token string">"Owl"</span><span class="token punctuation">,</span> <span class="token string">"Duck"</span><span class="token punctuation">,</span> <span class="token string">"Eagle"</span><span class="token punctuation">,</span> <span class="token string">"Bee"</span><span class="token punctuation">]</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Original Set:"</span><span class="token punctuation">,</span> setNames<span class="token punctuation">)</span>

<span class="token comment">// Element</span>
<span class="token keyword">var</span> newEle1 <span class="token operator">=</span> <span class="token string">"Owl"</span>
<span class="token keyword">var</span> newEle2 <span class="token operator">=</span> <span class="token string">"Parrot"</span>

<span class="token comment">// Remove element from the set</span>
<span class="token comment">// Here the given element is the part of the set</span>
<span class="token keyword">if</span> <span class="token keyword">let</span> res1 <span class="token operator">=</span> setNames<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token parameter">newEle1</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">"Removed Element 1: "</span><span class="token punctuation">,</span> res1<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment">// Remove element from the set</span>
<span class="token comment">// Here the given element is not the part of the set</span>
<span class="token keyword">if</span> <span class="token keyword">let</span> res2 <span class="token operator">=</span> setNames<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token parameter">newEle2</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">"Removed Element 1: "</span><span class="token punctuation">,</span> res2<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">"Removed Element 2: Nil"</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 Set:"</span><span class="token punctuation">,</span> setNames<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Original Set: ["Bee", "Eagle", "Hen", "Duck", "Owl"]
Removed Element 1: Owl
Removed Element 2: Nil
Modified Set: ["Bee", "Eagle", "Hen", "Duck"]

Here, in the above code, we have a set of String type named setNames. Now we remove two elements that are "owl" and "parrot" from the set using remove() function. When element is "owl" then it will remove "owl" from the set and return the removed element in optional type. Whereas when we remove "parrot" from the set, then it will return nil because parrot is not the part of setNames set. Here, the remove() function return element in optional type, so we use ! Or let to unwrap optional type. Hence the final set is ["Bee", "Eagle", "Hen", "Duck"].

Updated on: 2022-10-20T08:06:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements