April 2017
Intermediate to advanced
316 pages
9h 33m
English
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.
Read now
Unlock full access