Find index of the element differing in parity with all other array elements | Function to print the array element which differs in parity with the remaining array elements ; Multimaps to store even and odd numbers along with their indices ; Traverse the array ; If array element is even ; Otherwise ; If only one even element is present in the array ; If only one odd element is present in the array ; Given array ; Size of the array
function OddOneOut ( arr , N ) { let e = new Map ( ) ; let o = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { e . set ( arr [ i ] , i ) ; } else { o . set ( arr [ i ] , i ) ; } } if ( e . size == 1 ) { document . write ( Array . from ( e . values ( ) ) [ 0 ] ) ; } else { document . write ( Array . from ( o . values ( ) ) [ 0 ] ) ; } } let arr = [ 2 , 4 , 7 , 8 , 10 ] ; let N = arr . length ; OddOneOut ( arr , N ) ;