.insert()
Anonymous contributor
Published Nov 22, 2022
Contribute to Docs
The .insert()
method adds or inserts a given element in a set.
Syntax
setName.insert(element)
Since a set must contain unique elements, the element
parameter will only be inserted if it does not already exist in setName
.
Example
The following is an example of the .insert()
method:
var myFruitsSet: Set<String> = ["apple"]print(myFruitsSet.insert("banana"))print(myFruitsSet)print(myFruitsSet.insert("apple"))print(myFruitsSet)
This results in the following output:
(inserted: true, memberAfterInsert: "banana")["apple", "banana"](inserted: false, memberAfterInsert: "apple")["apple", "banana"]
Note: Sets are unordered collections, so the order of elements can vary.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.