I have a list of 300 numbers, they are not ordered. I would like to find the most efficient way to determine if a number is in that list.
The simple answer is in_array()
. This of course has a Big O of O(n).
But since I have complete control over this list, I believed I can make it faster.
So I made the list an associative array where key is the number I am looking for which an isset()
will yield me O(1).
$myArray = array('434342'=>true, '345235'=>true, '562211'=>true, '3333245'=>true, '99087782'=>true);
Is this the best way? Since the array size is small the hit of O(n) where n=300 its not worth the extra effort.