Find Duplicate Elements In Array Using Swift

后端 未结 12 1413
说谎
说谎 2020-11-29 01:42

How to find Duplicate Elements in Array? I have array of phone numbers so in the phone numbers i should start searching from the right side to the left side and find similar

12条回答
  •  时光说笑
    2020-11-29 02:13

    Feeling ~clever~. Given an array of Ints

    let x = [1, 1, 2, 3, 4, 5, 5]
    let duplicates = Array(Set(x.filter({ (i: Int) in x.filter({ $0 == i }).count > 1})))
    // [1, 5]
    

    Please note, this is horrendously efficient for everyone involved, including the compiler, and you.

    I'm just showing off.

    Edit: lol someone downvoted this, which leads me to reiterate, just in case: please DO NOT USE THIS in production or anywhere else.

提交回复
热议问题