xxxxxxxxxx
let fruits = ["apple", "pear", "pear", "banana", "apple"]
let noDuplicates = Array(Set(fruits)) // ["apple", "pear", "banana"]
//This is super short and works very well.
//It has one possibly big disadvantage and that is it loses the order of items.
//If you don’t care about order, then it does not matter.
// Otherwise just call sorted or sort as a last step like below
let orderedArray = noDuplicates.sorted()
xxxxxxxxxx
extension Sequence where Element: Hashable {
func uniqued() -> [Element] {
var set = Set<Element>()
return filter { set.insert($0).inserted }
}
}
func foobar() {
let myUniqueArray = [1,2,4,2,1].uniqued() // => [1,2,4]
}