Skip to content

Instantly share code, notes, and snippets.

@andrew-codechimp
Last active February 5, 2020 11:50
Show Gist options
  • Save andrew-codechimp/52ec5f1864ac9238c6bb756676e916c3 to your computer and use it in GitHub Desktop.
Save andrew-codechimp/52ec5f1864ac9238c6bb756676e916c3 to your computer and use it in GitHub Desktop.
[Get unique items in an array based on hash] #Swift
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