NishchalAndroid NishchalAndroid - 1 year ago 70
Swift Question

Add elements of two array with each other

I have 2 Array of type Int like this

let arrayFirst = [1,2,7,9]
let arraySecond = [4,5,17,20]

I want to add the elements of each array, like arrayFirst[0] + arraySecond[0], arrayFirst[1] + arraySecond[1] an so on and assign it to another array, so the result of the array would be like

[5, 7, 24, 29]

What would be the best practice to achieve this using

Answer Source

You can add both the arrays like this

let arrayFirst = [1,2,7,9]
let arraySecond = [4,5,17,20]

let result = zip(arrayFirst, arraySecond).map(+)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download