To remove a specific item from a set in Swift, you can use the remove method to delete the item from the set.
In this example,
numberSet
using the Set collection type in Swift, which contains integer values 1
, 2
, 3
, 4
, and 5
.remove
method of numberSet
to remove the item 3
from the set.numberSet
to see the removed item.var numberSet: Set<Int> = [1, 2, 3, 4, 5]
numberSet.remove(3)
for num in numberSet {
print(num)
}
1 2 4 5
In this example,
stringSet
using the Set collection type in Swift, which contains string values "apple"
, "banana"
, "cherry"
, "date"
, and "grape"
.remove
method of stringSet
to remove the item "cherry"
from the set.stringSet
to see the removed item.var stringSet: Set<String> = ["apple", "banana", "cherry", "date", "grape"]
stringSet.remove("cherry")
for str in stringSet {
print(str)
}
banana grape date apple
In this tutorial, we learned How to Remove Specific Item from a Set in Swift language with well detailed examples.