The O(n)
time is true even if there are duplicate elements. You should familiarize yourself with big-oh notation.
In the worst case, consider this array: 1, 1, 1, 1, ..., 1, 1, 2
. Searching for 2
will take exactly n
comparisons if you start from the first element, so having duplicates didn't help at all. If you were to search for 1
, you'd find it in a single comparison, but there are inputs of distinct elements for which you can also find an element in a single comparison if you're lucky, so having duplicates really doesn't mean much, except that you're more likely to get lucky and find your target element in fewer steps. It will still be O(n)
however.
There are almost always best cases and worst cases. The practical performance of most algorithms always depends on the given input, big-oh notation just gives you a vague idea of how the algorithm will perform. This isn't to say that asymptotic notation is useless, just that it's not always entirely accurate because there are underlying constants involved that do make a difference in practice.
If in doubt about performance, run your own benchmarks.