Created
December 18, 2019 00:15
-
-
Save fl034/29251b8fb6ee4a480ca6386202813e57 to your computer and use it in GitHub Desktop.
Removing duplicates in arrays with keypath
This file contains hidden or 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: 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