Swift – Remove a Value from Set
To remove a value from Set in Swift, call
Set.remove() and pass the value as argument to
Set.remove() removes/deletes the value from the Set, if the value is present. If the value is not present, the Set remains as is.
In the following program, we will take a Set of integer values, and remove a value from it using
var x: Set = [1, 4, 9] print("Original Set: \(x)") x.remove(4) print("After remove: \(x)")
Now, let us consider a scenario, where the value we would like to remove is not present in the Set.
var x: Set = [1, 4, 9] print("Original Set: \(x)") x.remove(16) print("After remove: \(x)")
In this Swift Tutorial, we have learned how to remove a value from Set using Set.remove().