78

I want to remove all elements of value x from an array that contains x, y and z elements

let arr = ['a', 'b', 'c', 'b']

How can I remove all elements of value 'b' from arr?

8 Answers 8

157

A filter:

 let farray = arr.filter {$0 != "b"} 
6
  • 4
    You don't even need the braces as it's a trailing closure :) arr = arr.filter {$0 != 'b'} <3 swift
    – sachadso
    Commented Sep 16, 2014 at 16:14
  • 1
    Just pointing out with regard to the above comment, without braces the compiler does throw 'Anonymous closure argument not contained in a closure'
    – user1078170
    Commented Sep 27, 2014 at 8:03
  • 4
    she/he meant parentheses, not braces
    – maltalef
    Commented Oct 28, 2014 at 15:36
  • 2
    with swift two, you can't use ', you would need to use " . so the answer will be let something = arr.filter{$0 != "b"} Commented Sep 12, 2015 at 14:41
  • 1
    I can't decide if even this one-liner is very legible or not.
    – Jonny
    Commented Sep 21, 2017 at 8:06
26

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).

1
  • This works as long as the type is Equatable, like strings (as in the original question). Unlike every other modern language in the world, Swift does not offer a method to this does with regular values.
    – user11638666
    Commented Feb 23, 2022 at 16:52
20
var array : [String]
array = ["one","two","one"]

let itemToRemove = "one"

while array.contains(itemToRemove) {
    if let itemToRemoveIndex = array.index(of: itemToRemove) {
        array.remove(at: itemToRemoveIndex)
    }
}

print(array)

Works on Swift 3.0.

7
  • 1
    Although this code is a little sloppy, it's about 45% faster than the filter solution Commented Nov 15, 2015 at 18:01
  • @AaronBrager I just made tests and it was 10% slower than filter. I guess comparing like that doesn't make much sense anyway since the results highly depend on how frequent and how evenly distributed are the items to remove in the source array.
    – Eric Aya
    Commented Nov 15, 2015 at 18:38
  • @EricD. Yeah I agree it depends on the source array (I used the one in the question.) Here's my test - filter was never faster gist.github.com/getaaron/11b751b15489b0b95e9e Commented Nov 15, 2015 at 18:52
  • I think will depend on how much you care if the value you are trying to remove it isn't there and if you want to do something if the value doesn't exist on the array
    – Nitrousjp
    Commented Nov 16, 2015 at 4:13
  • No neet to use contains and index(of:). It would be much faster to use just index(of) since it returns an optional.
    – Leo Dabus
    Commented Feb 3, 2018 at 20:35
8

EDITED according to comments:

I like this approach:

var arr = ["a", "b", "c", "b"]

while let idx = arr.index(of:"b") {
    arr.remove(at: idx)
}

Original answer (before editing):

let arr = ['a', 'b', 'c', 'b']

if let idx = arr.index(of:"b") {
    arr.remove(at: idx)
}
5
  • 2
    Note that this will remove only the first array element equal to 'b'. To remove all of them (as required in this question), replace if by while.
    – Martin R
    Commented Feb 3, 2018 at 16:45
  • 1
    Note that arr is declared as a constant you can't use a mutating method on it
    – Leo Dabus
    Commented Feb 3, 2018 at 20:31
  • 1
    Btw the single quote syntax used in the array declaration is wrong.
    – Leo Dabus
    Commented Feb 3, 2018 at 20:38
  • Martin and Leo are both right. Thx for pointing out. I have edited the answer. I must have never tried it because the constant declaration should not even compile. My bad.
    – Jens
    Commented Feb 5, 2018 at 14:14
  • 1
    This approach requires the type in Array<type> to be Equatable. Commented Apr 11, 2022 at 0:04
3

In Swift 3 I simply do:

arr = arr.filter { $0 != "a" } 

.filter, .sort and .map are great for saving time and solve lots of problems with little code.

This article has good examples and explain the differences and how they work: https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/

2

If you have more than one element to remove, thanks to first answer.

 var mainArray = ["a", "b", "qw", "qe"]
 let thingsToRemoveArray = ["qw", "b"] 


        for k in thingsToRemoveArray {
            mainArray =  mainArray.filter {$0 != k}
          }
1

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 this captures the value for that - which can be useful when that is no longer available.

0

If you only have one element of that value, this would probably be the easiest.

arr.remove(at: arr.index(of: ‘b’))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.