I have one array. I need to divide that array into two halves; first half in one array, second in another.
tried code:
let totalArray = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10]
var firstArray = [Int]()
var secondArray = [Int]()
for i in totalArray.indices {
if i <= totalArray.count/2 {
firstArray.append(contentsOf: [i])
} else {
secondArray.append(contentsOf: [i])
}
}
o/p:
[0, 1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
But I need it like this:
firstArray = [20, 19, 18, 17, 16, 15]
secondArray = [14, 13, 12, 11, 10]
What am I doing wrong?
firstArray.append(totalArray[i])