
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift Program to Add a New Element in the Set
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”].
- Related Articles
- Swift Program to Add Elements to a Set
- Swift Program to Search an Element in a Set
- Swift Program to Get a Random Element from the Set
- Swift Program to Remove an Element from the Set
- Swift Program to Find Minimum Set Element Using Library Function
- How to add a new element in the XML using PowerShell?
- Add an element to an array in Swift
- Swift Program to Initializing a Set
- Swift Program to Print a Set
- Swift Program to Reverse a Set
- How to add a new element to HTML DOM in JavaScript?
- Swift Program to Add Elements to a Dictionary
- MongoDB query to add new array element in document
- Swift Program to Search an Element in a Dictionary
- How to add a new list element under a using jQuery?
