How to remove an element of a given value from an array in Swift

前端 未结 7 1819
走了就别回头了
走了就别回头了 2020-12-28 11:34

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 r

7条回答
  •  我在风中等你
    2020-12-28 12:09

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

提交回复
热议问题