Store frequency of elements in matrix
for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) { if ( mp . has ( arr [ i ] [ j ] ) ) { mp . set ( arr [ i ] [ j ] , mp . get ( arr [ i ] [ j ] ) + 1 ) } else mp . set ( arr [ i ] [ j ] , 1 ) ; } }