
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add New Element in a Set in Swift
This tutorial will discuss how to write swift program to add a new element in 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 add a new element in the set Swift provide the following functions ?
insert()
update()
Below is a demonstration of the same ?
Input
Suppose our given input is ?
MySet = [23, 45, 1, 46, 2] Insert = 98
Output
The desired output would be ?
New Set = [23, 45, 1, 46, 2, 98]
Method 1 - Using insert() Function
To add a new element in the set Swift provide an in-built function named insert(). The insert() function is used to add new element in the specified set. This function does not return any value it just update the given set.
Syntax
Following is the syntax ?
SetName.insert(NewItem)
Algorithm
Following is the algorithm ?
Step 1? Create a set with values.
Step 2? Insert a new element using insert() function.
setElement.insert(100)
Step 3? Print the output
Example
The following program shows how to add new element in the set.
import Foundation import Glibc // Creating a set of integer type var setElement : Set = [34, 67, 89, 23, 12, 98, 1, 98, 13] print("Original Set:", setElement) // Insert new element setElement.insert(100) print("New Set:", setElement)
Output
Original Set: [12, 98, 13, 67, 23, 89, 1, 34] New Set: [12, 100, 98, 13, 67, 23, 89, 1, 34]
Here, in the above code, we have a set of integer type named setElement. Now we insert a new element that is 100 into it using insert() function ?
setElement.insert(100)
Hence the updated set is: [12, 100, 98, 13, 67, 23, 89, 1, 34].
Method 2 - Using update() Function
To add a new element in the set Swift provide another in-built function named update(). The update() function is used to add the given element in the specified set. This function does not return any value it just update the given set.
Syntax
Following is the syntax ?
SetName.update(with: myItem)
Algorithm
Following is the algorithm ?
Step 1? Create a set with values.
Step 2? Insert a new element using update() function.
setNames.update(with: "Robin")
Step 3? Print the output
Example
The following program shows how to add new element in the set.
import Foundation import Glibc // Creating a set of string type var setNames : Set = ["Hen", "Owl", "Duck", "Egale", "Bee"] print("Original Set:", setNames) // Insert new element setNames.update(with: "Robin") print("Updated Set:", setNames)
Output
Original Set: ["Duck", "Egale", "Bee", "Hen", "Owl"] Updated Set: ["Duck", "Robin", "Eagle", "Bee", "Hen", "Owl"]
Here, in the above code, we have a set of String type named setNames. Now we insert a new element that is "Robin" into it using update() function ?
setNames.update(with: "Robin")
Hence the updated set is: ["Duck", "Robin", "Eagle", "Bee", "Hen", "Owl"].