Last active
February 5, 2020 11:50
-
-
Save andrew-codechimp/52ec5f1864ac9238c6bb756676e916c3 to your computer and use it in GitHub Desktop.
[Get unique items in an array based on hash] #Swift
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 Sequence where Iterator.Element: Hashable { | |
func unique() -> [Iterator.Element] { | |
// Usage | |
// print(array.unique()) // prints: [1, 2, 3] | |
var seen: Set<Iterator.Element> = [] | |
return filter { seen.insert($0).inserted } | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment