I've the following code and I'm trying to get the time complexity.
seen = set()
a=[4,4,4,3,3,2,1,1,1,5,5]
result = []
for item in a:
if item not in seen:
seen.add(item)
result.append(item)
print (result)
As far as my understanding goes as I'm accessing the list the time complexity for that operation would be O(n)
. As with the if block each time I've a lookup to the set and that would cost another O(n)
. So is the overall time complexity O(n^2)
? Does the set.add()
also add to the complexity?
Also, with the space complexity is it O(n)
? Because the size of the set increases each time it encounters a new element?
Any input or links to get a proper insight into time and space complexities is appreciated.