I have a source array with an unknown number of elements:
let sourceArray = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]
I need to 'deal' those elements out into a given number of new arrays.
For example, given '3' empty arrays, the result would look like this:
let result = [["A", "D", "G"],["B", "E", "H"],["C", "F", "I"]]
This is what I've come up with, which technically works but feels pretty clunky. Any suggestions for improvement?
func createArrays(sourceArray: [String], count: Int) -> [[String]] {
var resultArrays = [[String]]()
for i in 0..<count {
var newArray = [String]()
var currentIndex = i
for (index, item) in sourceArray.enumerated() {
if index == currentIndex {
newArray.append(item)
currentIndex = currentIndex + count
}
}
resultArrays.append(newArray)
}
return resultArrays
}