Search code examples
c#dictionarytime-complexityspace-complexity

What is time and space complexity of Dictionary?


Let's assume I have data of size N (i.e.N elements) and the dictionary was created with capacity N. What is the complexity of:

  • space -- of entire dictionary
  • time -- adding entry to dictionary

MS revealed only that entry retrieval is close to O(1). But what about the rest?


Solution

  • The time complexity of adding a new entry is documented under Dictionary<T>.Add():

    If Count is less than the capacity, this method approaches an O(1) operation. If the capacity must be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.