Find Duplicate Elements In Array Using Swift

后端 未结 12 1414
说谎
说谎 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:16

    Antoine's solution in Swift 3+ syntax

    extension Array {
    
        func filterDuplicates(includeElement: @escaping (_ lhs: Element, _ rhs: Element) -> Bool) -> [Element] {
    
            var results = [Element]()
    
            forEach { (element) in
    
                let existingElements = results.filter {
                    return includeElement(element, $0)
                }
    
                if existingElements.count == 0 {
                    results.append(element)
                }
            }
            return results
        }
    }
    

提交回复
热议问题