mastodon-ios/Mastodon/Extension/Array+removeDuplicates.swift

24 lines
533 B
Swift

//
// Array+removeDuplicates.swift
// Mastodon
//
// Created by BradGao on 2021/3/31.
//
import Foundation
/// https://www.hackingwithswift.com/example-code/language/how-to-remove-duplicate-items-from-an-array
extension Array where Element: Hashable {
func removingDuplicates() -> [Element] {
var addedDict = [Element: Bool]()
return filter {
addedDict.updateValue(true, forKey: $0) == nil
}
}
mutating func removeDuplicates() {
self = self.removingDuplicates()
}
}