Find index of the element differing in parity with all other array elements | C # program for the above approach ; Function to print the element which differs in parity ; Stores the count of odd and even array elements encountered ; Stores the indices of the last odd and even array elements encountered ; Traverse the array ; If array element is even ; Otherwise ; If only one odd element is present in the array ; If only one even element is present in the array ; Driver code ; Given array ; Size of the array
using System ; class GFG { static void oddOneOut ( int [ ] arr , int N ) { int odd = 0 , even = 0 ; int lastOdd = 0 , lastEven = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { even ++ ; lastEven = i ; } else { odd ++ ; lastOdd = i ; } } if ( odd == 1 ) { Console . WriteLine ( lastOdd ) ; } else { Console . WriteLine ( lastEven ) ; } } static void Main ( ) { int [ ] arr = { 2 , 4 , 7 , 8 , 10 } ; int N = arr . Length ; oddOneOut ( arr , N ) ; } }