If you go to leetcode.com/problems/find-the-duplicate-number/solution/ (problem 287) there is the following solution given:
def findDuplicate(self, nums):
seen = set()
for num in nums:
if num in seen:
return num
seen.add(num)
The time complexity of this solution is stated as O(n)
I'm trying to figure out why that is the case. The way I'm thinking of it is if you're given the following list:
x= [1,2,3,4,5,6,7,8,9,10,11,....98,99,100,100] i.e. numbers constantly increasing (or not being the same) up to the duplicated number at the end of the array
Then as you iterate through each element, you have an ever increasing size of the set to look through for the duplicate value. Wouldn't this be longer than O(N) time?
Specifically, if you try to look for 98 in the set, you have to look through 97 (N-4) values to see it's not in the set, then add it in. For 99, you look through 98 values, etc. It seems like an O(N^2) solution in the worst case?