Swift开发-数组去重

91 阅读1分钟

import Foundation

//数组去重
extension Array where Element: Hashable {

    func removingDuplicates() -> [Element] {

        var dict = [Element: Bool]()

        return filter {

            dict.updateValue(true, forKey: $0) == nil

        }

    }

    mutating func removeDuplicates() {

        self = self.removingDuplicates()

    }

}