Time and space complexity for removing duplicates from a list

吃可爱长大的小学妹 提交于 2021-02-08 02:57:08

问题


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.


回答1:


Sets in Python are implemented as hash tables (similar to dictionaries), so both in and set.add() are O(1). list.append() is also O(1) amortized.

Altogether, that means the time complexity is O(n), due to the iteration over a.

Space complexity is O(n) as well, because the maximum space required is proportional to the size of the input.

A useful reference for the time complexity of various operations on Python collections can by found at https://wiki.python.org/moin/TimeComplexity … and the PyCon talk The Mighty Dictionary provides an interesting delve into how Python achieves O(1) complexity for various set and dict operations.



来源:https://stackoverflow.com/questions/42710045/time-and-space-complexity-for-removing-duplicates-from-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!