ARTICLE AD BOX
Asked today
Viewed 19 times
i have a multidimensional Int Array. I want to check each array with each array but without repeating same operations like comparing myArr[4] with myArr[5] and later myArr[5] with myArr[4] .
My goal is to find the arrays with at least one equal number, probable by converting the arrays to sets and look for intersections.
Can i use a higher order function like reduce,map,filter,sort with a closure?
1471 silver badge6 bronze badges
1
Yes you can use sets and map function to implement this for e.g like this
Assuming you have 2D arraylist
let arr = [[1, 2, 4], [4, 9, 10], [7, 8, 11], [2, 30, 50]] let sets = arr.map { Set($0) } for firstIdx in 0..<sets.count { for secondIdx in (firstIdx+1)..<sets.count { if !sets[firstIdx].isDisjoint(with: sets[secondIdx]) { print("Found matching element between one array at index \(firstIdx) and another array at index \(secondIdx)") } } }Explore related questions
See similar questions with these tags.
