How to create a collection with O (1) complexity

I would like to create a data structure or collection that will have O (1) complexity when adding, deleting and evaluating no. elements. How should i start?

I thought of a solution: I will use a Hashtable and for each key / value pair inserted, I will have only one hash code, that is: my hash code algorithm will generate a unique hash value each time, so every time, the index in which stores the value will be unique (i.e. no collisions).

Will it give me O (1) complexity?

+3
source share
7 answers

, , , , 100% . , - . .

: Hashmap.size() O (1)

edit 2: Respopnse , = P

, - O (k), k - . . , , O (1) . , O (1) . , .

: . , , , O (1) . , , , , .

, O (1).

, , O (1) Big O. .

, , , , "" .

, - ( ) ..

, .

+3

, ( , , ) . . .

+2

- , , , . , . , , , .

, - 1 Java HashMap . , , hashCode() , Java. , . , O (1), .

1 -, .

+1

- , . , - . - , .

. , A (: 2), B (: 18), C (hash: 66) . , HashMap 16 ( ). % ​​16 ( ) -, A (: 2% 16 = 2), B (: 18% 16 = 2), C ( : 66% 16 = 2)

HashMap , Hashtable, . ( CopncurrentHashMap) IMHO, Hashtable 12 , , .

+1

, ?

0

, , , . , - (1, n). "-" (+ )

, . , , , - .

0

. -. . Hashmap - O (1) , , .

, - O (1), Hashmap , ( ).

, Hash , . , .

. , , . , O (1) .

-3
source

Source: https://habr.com/ru/post/1756934/


All Articles