check if the current element is equal to previous element .
if ( ele [ idx - 1 ] == ele [ idx ] ) { freq ++ ; idx ++ ; } else { document . write ( element + " " + freq ) ; element = ele [ idx ] ; idx ++ ;