How can I sum the positions of individual elements in int []?

Suppose I have the following:

var a1 = new [] { 2, 7, 9 };
var a2 = new [] { 6, 3, 6 };

I want to end up with:

var sum = new [] { 8, 10, 15 };

What is the fastest way to get there?

+4
source share
1 answer

You can use Zip():

var res = a1.Zip(a2, (x, y) => x + y).ToArray();

Alternatively, you can use Select():

var res = a1.Select((x, i) => x + a2[i]).ToArray();
+6
source

Source: https://habr.com/ru/post/1670875/


All Articles