Again traverse the array and find the actual counts .
for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] === first ) count1 ++ ; else if ( arr [ i ] === second ) count2 ++ ; } if ( count1 > n / 3 ) { document . write ( first + " " ) ; flag = 1 ; } if ( count2 > n / 3 ) { document . write ( second + " " ) ; flag = 1 ; } if ( flag === 0 ) { document . write ( " " + " " ) ; } }