array_unique vs array_flip

I benchmarked it for you: CodePad Your intuition on this was correct! $test=array(); for($run=0; $run<1000; $run++) $test[]=rand(0,100); $time=microtime(true); for($run=0; $run<100; $run++) $out=array_unique($test); $time=microtime(true)-$time; echo ‘Array Unique: ‘.$time.”\n”; $time=microtime(true); for($run=0; $run<100; $run++) $out=array_keys(array_flip($test)); $time=microtime(true)-$time; echo ‘Keys Flip: ‘.$time.”\n”; $time=microtime(true); for($run=0; $run<100; $run++) $out=array_flip(array_flip($test)); $time=microtime(true)-$time; echo ‘Flip Flip: ‘.$time.”\n”; Output: Array Unique: 1.1829199790955 Keys Flip: 0.0084578990936279 Flip … Read more

Can an O(n) algorithm ever exceed O(n^2) in terms of computation time?

Asymptotic complexity (which is what both big-O and big-Theta represent) completely ignores the constant factors involved – it’s only intended to give an indication of how running time will change as the size of the input gets larger. So it’s certainly possible that an Θ(n) algorithm can take longer than an Θ(n2) one for some … Read more

What is the complexity of this simple piece of code?

This seems to be a question of mislead, because I happened to read that book just now. This part of text in the book is a typo! Here is the context: =================================================================== Question: What is the running time of this code? 1 public String makeSentence(String[] words) { 2 StringBuffer sentence = new StringBuffer(); 3 for … Read more