49

I've encountered a purely hypothetical problem which feels like it has an easy solution if I find the right linq method...

I have two arrays of ints and I know they are the same size. I want to create a third array of the same size where the elements in the third array are the sum of the elements in the first two arrays in the corresponding position.

Below is a method that should show what I want to do.

public static int[] AddArrays(int[] a, int[] b)
{
    int[] newArray = new int[a.Length];
    for (int i = 0; i<a.Length; i++)
    {
        newArray[i]=a[i]+b[i];
    }
    return newArray;
}

Are there any Linq methods that I can just use like

return a.DoStuff(b, (x,y) => x+y)

or something like that?

I should note that this probably falls in the category of homework since the original problem came from a website I was looking at (though I can't find a direct link to the problem) and not as a question I need for work or anything.

If no simple method exists then what is the most Linqy way to do this? an array.each would seem to have the problem of not being able to index the second array easily to add the values to the one you are iterating through leading me to wonder if Linq would be any help at all in that situation...

3
  • 1
    FYI, this is usually called a “zip” (the zipper analogy should be obvious) and Linq has no built-in support for it, except maybe via SelectMany. /EDIT Apparently no longer true in the current version. Commented Sep 9, 2011 at 9:59
  • 1
    Zip these guys have answered is avail only on .NET 4 Commented Sep 9, 2011 at 10:06
  • @zenwalker : yep and this is why better to upgrade Framework version as soon as possible, very neat features are ;) Commented Sep 9, 2011 at 10:08

6 Answers 6

79

Zip it :)

var a = new int[] {1,2,3 };
var b = new int[] {4,5,6 };
a.Zip(b, (x, y) => x + y)
Sign up to request clarification or add additional context in comments.

1 Comment

Zipping it is actually MUCH slower than doing it the way OP used to do with a normal loop...
20

You can use the Select method.

int[] a = new[] { 1, 2, 3 };
int[] b = new[] { 10, 20, 30 };

var c = a.Select ((x, index) => x + b[index]).ToArray();

3 Comments

Who will pass in and then increment the index?
The index is provided by the LINQ process.
That is a really cool feature I never knew about. Thanks for letting me know. Sadly however I think zip is a closer match to what I was after so I can't give you the accept. :(
6
public static int[] AddArrays(int[] a, int[] b)

{
     return a.Zip(b, (x,y) => x+y).ToArray();
}

Comments

2
IList<int> first = new List<int> { 2, 3, 4, 5 };
IList<int> second = new List<int> { 2, 3, 4, 5 };

var result = Enumerable.Zip(first, second, (a, b) => a + b);

Comments

2

Without LINQ:

private static IEnumerable<int> AddArrays(IEnumerable<int> a1, IEnumerable<int> a2)
{
    var e1 = a1.GetEnumerator();
    var e2 = a2.GetEnumerator();

    while (e1.MoveNext() && e2.MoveNext())
        yield return e1.Current + e2.Current;
}

Comments

1

If you have an IEnumerable<int[]> arrayCollection to sum:

arrayCollection.Aggregate((a,b) => a.Zip(b, (x,y) => x + y).ToArray())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.