Map, FlatMap, Reduce & More

Unique

released Wed, 20 Feb 2019
Swift Version 5.0

Unique

Return a list with all duplicates removed. The better solution would be to use a Set.

[1, 2, 5, 1, 7].reduce([], { (a: [Int], b: Int) -> [Int] in

   if a.contains(b) {

     return a

   } else {

     return a + [b]

   }

})

// prints: 1, 2, 5, 7