0

What is the computational complexity of the following operation:

list1 = [1,2,2,2,3,4,4]
set1 = set(list)
martineau
  • 119,623
  • 25
  • 170
  • 301
Daniel Chepenko
  • 2,229
  • 7
  • 30
  • 56
  • wouldn't that be implementation dependent? (but probably O(N)) – Mitch Wheat Jan 23 '18 at 03:24
  • 1
    Appears this has been answered before: https://stackoverflow.com/questions/34642155/what-is-time-complexity-of-the-python-list-to-set-conversion – Nima Jan 23 '18 at 03:26

1 Answers1

0

It is O(n), where n is the number of elements in the list. set() is essentially iterating over the list and adding each of those elements to a hash table.

cs95
  • 379,657
  • 97
  • 704
  • 746
Surya Avala
  • 157
  • 10