# As of 2 arrays to get one combo in Swift?

There are 2 arrays:
``````var array1: [String] = ["l1", "m2", "r3"]
var array2: [String] = ["l4", "m5", "r6"]
var combinations: [[String]] = []``````

Need to combine array1 and array2 so that the combinations happened in the end:
``````var combinations: [[String]] = [
["l1", "m2", "r3"], ["l1", "m5", "r3"], ["l1", "m5", "r6"], ["l1", "m2", "r6"],
["l4", "m2", "r3"], ["l4", "m5", "r3"], ["l4", "m5", "r6"], ["l4", "m2", "r6"]
]``````
March 23rd 20 at 19:02
March 23rd 20 at 19:04
Solution
``````let arrays: [[String]] = [array1, array2]
var combinations: [[String]] = []

let arraysLen: Int = arrays.count
let arrLen: Int = arrays[0].count
let numCombinations: Int = Int(pow(Double(arraysLen), Double(arrLen)))

for i in (0 ..< numCombinations) {
var combination: [String] = []
for j in (0 ..< arrLen) {
let arrIndex: Int = i / Int(pow(Double(arraysLen), Double(j))) % arraysLen
let elIndex: Int = arrLen - j - 1
combination.insert(arrays[arrIndex][elIndex] at: 0)
}
combinations.append(combination)
}``````

Find more questions by tags Swift