Store all the elements in the map with their occurrence
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 ) ; }