-
-
Save amomchilov/fbba1e58c91fbd4b5b767bcf8586112b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array { | |
func removingDuplicates<T: Hashable>(byKey key: KeyPath<Element, T>) -> [Element] { | |
var seenKeys = Set<T>() | |
return self.filter { candidate in seenKeys.insert(candidate[keyPath: key]).inserted } | |
} | |
func removingDuplicates<T: Hashable>(byKey deriveKey: (Element) -> T) -> [Element] { | |
var seenKeys = Set<T>() | |
return self.filter { candidate in seenKeys.insert(deriveKey(candidate)).inserted } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment