There is an algorithm that finds a unique element in the list:
#!/usr/bin/env python3 def find_uniq_1(arr): a, b = set(arr) # O(len(arr)) return a if arr.count(a) == 1 else b # O(n) print(find_uniq_1([ 1, 1, 1, 2, 1, 1 ])) It consists of only two lines of code. I determined the complexity of each line, but I find it difficult to choose the one that more influences the final complexity of the algorithm. Help me please.
I tried to increase the number of arguments and measure the execution time. As a result, I see that the graph grows almost linearly. That is, O (n) is most likely what I need. But I don’t understand why O (n) should have preferred O (len (arr)).
Are there any rules on this?
n- MBo 6:51 pm