Skip to content

Instantly share code, notes, and snippets.

@amomchilov
Created March 21, 2018 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amomchilov/fbba1e58c91fbd4b5b767bcf8586112b to your computer and use it in GitHub Desktop.
Save amomchilov/fbba1e58c91fbd4b5b767bcf8586112b to your computer and use it in GitHub Desktop.
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