Swift – Remove a Value from Set
To remove a value from Set in Swift, call Set.remove()
and pass the value as argument to remove()
.
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.
Examples
In the following program, we will take a Set of integer values, and remove a value from it using remove()
.
main.swift
var x: Set = [1, 4, 9] print("Original Set: \(x)") x.remove(4) print("After remove: \(x)")
Output

Now, let us consider a scenario, where the value we would like to remove is not present in the Set.
main.swift
var x: Set = [1, 4, 9] print("Original Set: \(x)") x.remove(16) print("After remove: \(x)")
Output

ADVERTISEMENT
Conclusion
In this Swift Tutorial, we have learned how to remove a value from Set using Set.remove().