kemchenj
9/12/2017 - 10:16 AM

Frequencies

通过给 Sequence 添加 extension 去检测 Element 的频率

https://twitter.com/chriseidhof/status/906148040020553728

let result = "hello".frequencies.filter { $0.value > 1 }
print(result) // ["l": 2]
import Foundation

extension Sequence where Element: Hashable {
    var frequencies: [Element: Int] {
        return Dictionary(lazy.map { ($0: 1) }, uniquingKeysWith: +)
    }
}