A general approach is to exploit first class procedures. (However, this approach is much more powerful than what is required for your question.) To illustrate, say you want to avoid "Justin" repeatedly in many collections.
let avoidJustin = notEqualTester ("Justin")
let arrayOfUsers = // ...
arrayOfUsers.filter (avoidJustin)
let arrayOfFriends = // ...
arrayOfFriends.filter (avoidJustin)
With this, you avoid repeatedly creating a closure each time you want to avoid Justin. Here is notEqualTester which, given a that, returns a function of this that returns this != that.
func notEqualTester<T: Equatable> (that:T) -> ((this:T) -> Bool) {
return { (this:T) -> Bool in return this != that }
}
The returned closure for thiscaptures the value for that - which can be useful when that is no longer available.
If you need to modify initial array, you can use the function removeAll(where:) that is available in Swift 4.2/Xcode 10:
var arr = ["a", "b", "c", "b"]
arr.removeAll(where: { $0 == "b" })
print(arr) // output is ["a", "c"]
However, if you are using Xcode 9 you can find this function in Xcode9to10Preparation (this library provides implementations of some new functions from Xcode 10).