Created
August 23, 2023 04:12
-
-
Save dagronf/07164ac9f9e2df95d445c09349325615 to your computer and use it in GitHub Desktop.
Return the unique elements of a sequence while maintaining the order of the input sequence
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 Sequence where Element: Equatable { | |
/// Return the unique elements in the array using Equatable as the predicate | |
var unique: [Element] { | |
return self.reduce(into: []) { uniqueElements, element in | |
if !uniqueElements.contains(element) { | |
uniqueElements.append(element) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment