I have an set of array like this [ [1,2], [2, 3], [4,5], [5, 6,], [7, 8], [1,8,9]].if one item intersect with another then union those items.But I get duplicate items like [[1,2], [2,3], [1,8,9],[1,2,3,8,9][4,5],[5,6], [4,5,6],[7,8]], Where those set of items [1,2],[2,3][1,8,9],[4,5],[5,6],[7,8] are duplicate.
I tried to use reduce method but the output I expected still didn't get it.
var newtotalOverlapingSet = Set<[UICollectionViewLayoutAttributes]>()
if totoalOverLapArray.count > 0{
let _ = totoalOverLapArray.reduce(totoalOverLapArray[0]){ firstSet, secondSet in
if firstSet.intersection(secondSet).count > 0{
newtotalOverlapingSet.insert(Array(firstSet.union(secondSet)))
return firstSet.union(secondSet)
}
return secondSet
}}
Here is my expected output [[1,2,3,7,8,9], [4, 5, 6] ] want to achieve.