Find Duplicate Elements In Array Using Swift

后端 未结 12 1407
说谎
说谎 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 01:56

    Based on Rob's answer, an array extension to just finds duplicates is:

    extension Array where Element: Hashable {
        func duplicates() -> Array {
            let groups = Dictionary(grouping: self, by: {$0})
            let duplicateGroups = groups.filter {$1.count > 1}
            let duplicates = Array(duplicateGroups.keys)
            return duplicates
        }
    }
    

提交回复
热议问题