Created
January 22, 2017 22:17
-
-
Save SAllen0400/98f648b1b7b0014eb36552ab25f8795f to your computer and use it in GitHub Desktop.
Filter Duplicates out of array
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
// 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