Elements that occurred only once in the array | Function to find the elements that appeared only once in the array ; Store all the elements in the map with their occurrence ; Traverse the map and print all the elements with occurrence 1 ; Driver code
function occurredOnce ( arr , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , 1 + mp . get ( arr [ i ] ) ) ; else mp . set ( arr [ i ] , 1 ) ; } for ( let [ key , value ] of mp . entries ( ) ) { if ( value == 1 ) document . write ( key + " " ) ; } } let arr = [ 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 ] ; let n = arr . length ; occurredOnce ( arr , n ) ;