Skip to content

Instantly share code, notes, and snippets.

@Denismih
Created May 24, 2019 10:27
Show Gist options
  • Save Denismih/4544d9d855c3eb225be7097ede44927c to your computer and use it in GitHub Desktop.
Save Denismih/4544d9d855c3eb225be7097ede44927c to your computer and use it in GitHub Desktop.
extension Array{
public mutating func appendDistinct<S>(contentsOf newElements: S, where condition:@escaping (Element, Element) -> Bool) where S : Sequence, Element == S.Element {
newElements.forEach { (item) in
if !(self.contains(where: { (selfItem) -> Bool in
return !condition(selfItem, item)
})) {
self.append(item)
}
}
}
}
var accounts: [Account]
let arrayToAppend: [Account]
accounts.appendDistinct(contentsOf: arrayToAppend, where: { (account1, account2) -> Bool in
return account1.number != account2.number
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment