Skip to content

Instantly share code, notes, and snippets.

@SAllen0400
Created January 22, 2017 22:17
Show Gist options
  • Save SAllen0400/98f648b1b7b0014eb36552ab25f8795f to your computer and use it in GitHub Desktop.
Save SAllen0400/98f648b1b7b0014eb36552ab25f8795f to your computer and use it in GitHub Desktop.
Filter Duplicates out of array
// Swift 3
extension Array {
func filterDuplicates(includeElement: @escaping (_ lhs: Element, _ rhs: Element) -> Bool) -> [Element] {
var results = [Element]()
forEach { (element) in
let existingElements = results.filter {
return includeElement(element, $0)
}
if existingElements.count == 0 {
results.append(element)
}
}
return results
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment