How do I sum a list<> of arrays

后端 未结 7 1476
故里飘歌
故里飘歌 2021-02-04 08:24

I have a List< int[] > myList, where I know that all the int[] arrays are the same length - for the sake of argument, let us say I have 500 arrays, each is 2048 elements long

7条回答
  •  忘了有多久
    2021-02-04 08:50

    It can be done with Zip and Aggregate. The question is so old that probably Zip was not around at the time. Anyway, here is my version, hoping it will help someone.

    List myListOfIntArrays = PopulateListOfArraysOf100Ints();
    int[] totals = new int[100];
    int[] allArraysSum = myListOfIntArrays.Aggregate(
        totals,
        (arrCumul, arrItem) => arrCumul.Zip(arrItem, (a, b) => a + b))
        .ToArray();
    

提交回复
热议问题