Skip to content

Instantly share code, notes, and snippets.

@dagronf
Created August 23, 2023 04:12
Show Gist options
  • Save dagronf/07164ac9f9e2df95d445c09349325615 to your computer and use it in GitHub Desktop.
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
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