Skip to content

Instantly share code, notes, and snippets.

@fl034
Created December 18, 2019 00:15
Show Gist options
  • Save fl034/29251b8fb6ee4a480ca6386202813e57 to your computer and use it in GitHub Desktop.
Save fl034/29251b8fb6ee4a480ca6386202813e57 to your computer and use it in GitHub Desktop.
Removing duplicates in arrays with keypath
extension Array {
func removingDuplicates<T: Equatable>(uniqueProperty: KeyPath<Element, T>) -> Self {
var newArray = Self()
for item in self {
if !newArray.contains(where: { $0[keyPath: uniqueProperty] == item[keyPath: uniqueProperty] }) {
newArray.append(item)
}
}
return newArray
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment