zhi zhou zhi zhou - 2 years ago 122
Swift Question

How to filter duplicate elements and create new array?

I have a duplicate elements array


let array = [1, 2, 2, 3, 5, 3, 6]


How to filter this array and create new array like this:


[[1], [2, 2], [3, 3], [5], [6]]

Answer Source

Quite easy, you should try with this:

let array = [1, 2, 2, 3, 5, 3, 6]

let result= Set(array).map{ value in return array.filter{$0==value} }

print(result)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download