Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int[] arr1 = {1, 2, 3, 4, 5}; // {1, 2, 3, 4, 5}
- int[] arr2 = {1, 2}; // {1, 2}
- int[] sumArr12 = arr1.Select((value, index) => value + arr2[index % arr2.Length]).ToArray(); // {2, 4, 4, 6, 6}
- int[] arrOne = { 1, 2, 3, 4, 5 };
- int[] arrTwo = { 1, 2 };
- int[] result = new int [arrOne.Length > arrTwo.Length ? arrOne.Length : arrTwo.Length];
- var firstIndex = 0;
- var secondIndex = 0;
- for(int index = 0; index < result.Length; index++)
- {
- firstIndex = arrOne.Length > firstIndex ? firstIndex : 0;
- secondIndex = arrTwo.Length > secondIndex ? secondIndex : 0;
- result[index] = arrOne[firstIndex++] + arrTwo[secondIndex++];
- }
- //result = {2, 4, 4, 6, 6}
- int[] arrOne = { 1, 2, 3 };
- int[] arrTwo = { 1, 2, 3, 4, 5, 6 };
- static IEnumerable<T> Looped<T>(T[] array)
- {
- for (int i = 0; ; i = (i + 1) % array.Length)
- yield return array[i];
- }
- int[] ar1 = { 1, 2, 3, 4, 5 };
- int[] ar2 = { 1, 2 };
- var ar3 = ar1.Zip(Looped(ar2), (x, y) => x + y).ToArray();
Add Comment
Please, Sign In to add comment