Find the Number Occurring Odd Number of Times | function to find the element occurring odd number of times ; Putting all elements into the HashMap ; If array element is already present then increase the count of that element . ; if array element is not present then put element into the HashMap and initialize the count to one . ; Checking for odd occurrence of each element present in the HashMap ; driver code
function getOddOccurrence ( arr , n ) { let hmap = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( hmap . has ( arr [ i ] ) ) { let val = hmap . get ( arr [ i ] ) ; hmap . set ( arr [ i ] , val + 1 ) ; } else { hmap . set ( arr [ i ] , 1 ) ; } } for ( let [ key , value ] of hmap . entries ( ) ) { if ( hmap . get ( key ) % 2 != 0 ) return key ; } return - 1 ; } let arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ] ; let n = arr . length ; document . write ( getOddOccurrence ( arr , n ) ) ;