To store the count of each a [ i ] in a map
var cnt = { } ; for ( var i = 0 ; i < n ; i ++ ) { if ( cnt . hasOwnProperty ( arr [ i ] ) ) { cnt [ arr [ i ] ] = cnt [ arr [ i ] ] + 1 ; } else { cnt [ arr [ i ] ] = 1 ; } }