Find index of the element differing in parity with all other array elements | 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 ; Given array ; Size of the array
function oddOneOut ( arr , N ) { let odd = 0 , even = 0 ; let lastOdd = 0 , lastEven = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { even ++ ; lastEven = i ; } else { odd ++ ; lastOdd = i ; } } if ( odd == 1 ) { document . write ( lastOdd ) ; } else { document . write ( lastEven ) ; } } let arr = [ 2 , 4 , 7 , 8 , 10 ] ; let N = arr . length ; oddOneOut ( arr , N ) ;