47

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. Sep 9, 2011 at 9:59
  • 1
    Zip these guys have answered is avail only on .NET 4
    – Zenwalker
    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 ;)
    – sll
    Sep 9, 2011 at 10:08

6 Answers 6

77

Zip it :)

var a = new int[] {1,2,3 };
var b = new int[] {4,5,6 };
a.Zip(b, (x, y) => x + y)
0
18

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
  • 1
    Who will pass in and then increment the index?
    – sll
    Sep 9, 2011 at 10:04
  • 3
    The index is provided by the LINQ process. Sep 9, 2011 at 10:10
  • 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. :(
    – Chris
    Sep 9, 2011 at 13:07
6
public static int[] AddArrays(int[] a, int[] b)

{
     return a.Zip(b, (x,y) => x+y).ToArray();
}
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);
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;
}
1

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

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.