Computing frequencies of all elements
var freq = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 1 && arr [ i ] > MAX ) { document . write ( " " ) ; return - 1 ; } freq [ arr [ i ] ] += 1 ; }