Search code examples
listdictionarycomplexity-theory

When are lists algorithmically faster than maps?


I think this is a valid question because if you use a Map with integers as keys you have a similar structure as a list. You can read elements in order with a for loop:

for i in 1,..., map.length():
    if i in map:
        doSomething(map[i])

Besides, inserting in list and reading an element is O(n) while inserting and reading in a map is O(1). In what case are lists faster than maps? If we are concerned about how fast the code runs, in what cases are the lists not strictly worse than maps? Should we always use maps in that case?

Wouldn't a list implemented with a map be a better list?


Solution

  • I think that fundamentally, a list is a particular implementation of a hash map. For this reason maps can only be better than lists. In theoretical complexity terms.