We can use reduce to remove duplicate elements in an array as follows:
let arrWithDuplicates = [1, 1, 2, 3, 3, 4, 4, 5, 6, 7]
let arrWithoutDuplicates = arrWithDuplicates.reduce([]) {
(a: [Int], b: Int) -> [Int] in
if a.contains(b) {
return a
} else {
return a + [b]
}
}
print(arrWithoutDuplicates)
The result will be [1, 2, 3, 4, 5, 6, 7], as expected.