How to split an array to 2 arrays with odd and even indices respectively? For example
int[] a = new int[]{1, 3, 7, 8};
then get two arrays
a1: {1, 7}
a2: {3, 8}
Simple using the overload of Where than contains the index which:
Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function.
int[] a = new int[] { 1, 3, 7, 8 };
int[] aEven = a.Where((x, i) => i % 2 == 0).ToArray();
int[] aOdd = a.Where((x, i) => i % 2 != 0).ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With