Created
August 28, 2020 16:53
-
-
Save olgusirman/732e299aab0e7ef03153866accfec21c to your computer and use it in GitHub Desktop.
Remove duplicates
This file contains 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
public extension Array where Element: Hashable { | |
func removeDuplicates() -> [Element] { | |
var items = Set<Element> | |
return self.filter { items.insert($0).inserted } | |
} | |
} | |
// [1,2,5,5,2,4,3].removeDuplicates() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment