Again traverse the array and find the actual counts .
for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == first ) count1 ++ ; else if ( arr [ i ] == second ) count2 ++ ; } if ( count1 > parseInt ( n / 3 , 10 ) ) return first ; if ( count2 > parseInt ( n / 3 , 10 ) ) return second ; return - 1 ; }