Traverse hash array build above until we reach k - th smallest element .
$ count = 0 ; for ( $ i = 0 ; $ i <= $ max_val ; $ i ++ ) { while ( $ hash [ $ i ] > 0 ) { $ count ++ ; if ( $ count == $ k ) return $ i ; $ hash [ $ i ] -- ; } } return -1 ; }