I have this function that dedups arrays by returning an array of its set.
func dedup<T: Hashable>(_ a: [T]) -> [T] {
return Array(Set(a))
}
let dirty = ["apple", "apple", "kiwi", "mango"]
let clean = dedup(dirty)
print(clean)
However, I can't figure out how to make this an extension of Array
.
extension Array where Element: Hashable {
static func deduped() -> Array {
return Array(Set<Element>(self))
}
}
The error I get is that self
does not conform to expected type Sequence
.