Data structure that allows accessing elements by index and delete them in O(1)

前端 未结 4 1502
眼角桃花
眼角桃花 2021-01-05 15:40

I have following task (as part of bigger task):

I need to take an k element from array like data structure and delete it (k is any possible index). Arra

4条回答
  •  伪装坚强ぢ
    2021-01-05 16:07

    The only data-structure that has a small overhead in adding and removing element is an hashtable. The only overhead is the cost of the hash function (and it is considered as O(1), if you take a purely theoretic approach).

    But, if you want it to be extremely efficient, you will need to:

    • Have an approximate of the number of elements you will have to get inside your data-structure (and allocate this number once for all at the beginning).
    • Choose an hash function that will avoid collision given the way your keys are distributed (collisions are just breaking the efficiency of hashtables).

    If you manage to get everything right, then you should be optimal.

提交回复
热议问题