The problem is that the time complexity of your code is O(n^2). Your code iterates over all numbers in the list, and for each number arr.count()
is called. The method arr.count()
will have to iterate the full list again to count the number of times the item occurs. So if the list contains n
numbers, this code will have to perform n * n
operations.
This means that if the list will become 1000 times larger, it will take 1000000 times longer to solve. In coding problems like this, the test set usually contains at least one list that is so long that it will timeout when using such algorithm, but not when using a more optimized algorithm.
In this specific programming exercise, you know that each list contains only 2 unique numbers. One number occurs exactly 1 time, the other number occurs (n - 1) times.
You could use a set
to find the 2 unique numbers in O(n) time and then iterate over only those 2 items instead of over all n items:
def find_uniq(arr):
for number in set(arr): # using set here!
if arr.count(number) == 1:
return number
The correctness and performance of this function can be tested with the following code:
# Verify code with example input:
find_uniq([ 1, 1, 1, 2, 1, 1 ]) == 2
find_uniq([ 0, 0, 0.55, 0, 0 ]) == 0.55
# Verify performance using a huge array (~1M elements):
huge_array = [1] * 1000000 + [2]
find_uniq(huge_array) == 2