I think the problem description should be made a little more specific; the array must contain only positive integers, and must have no duplicates. With that in mind, you don't have to check $array[0] through $array[$n]. If $array[$n] == $n+1 -- e.g., if $array[25] is 26 -- then you know that there are no missing numbers in @array[0..$n].