How to find Duplicate Elements in Array? I have array of phone numbers so in the phone numbers i should start searching from the right side to the left side and find similar
I've found a way by using reduce, here is the code(Swift 4):
let testNumbers = [1,1,2,3,4,5,2]
let nondupicate = testNumbers.reduce(into: [Int]()) {
if !$0.contains($1) {
$0.append($1)
} else {
print("Found dupicate: \($1)")
}
}
As a side effect, it returns an array has no dupicated elements.
You can easily modify it for counting duplicated elements numbers, checking string arrays etc.