Problem · Hash Table
Design Hash Map
Learn this problemProblem statement
Implement a hash map that stores integer values under integer keys. Build the storage yourself without using a language's built-in hash table, map, or dictionary for the stored entries.
Process the arrays from left to right. At index i:
"put"storesvalues[i]underkeys[i]. If the key already exists, replace its value."get"appends the value stored underkeys[i]to the result, or appends-1when the key is absent."remove"deleteskeys[i]when present. Removing a missing key has no effect.
Return an integer array containing the results of all get operations in encounter order.
Function
runHashMap(operations: String[], keys: int[], values: int[]) → int[]Examples
Example 1
operations = ["put","put","get","get","put","get","remove","get"]keys = [1,2,1,3,2,2,2,2]values = [10,20,0,0,25,0,0,0]return = [10,-1,25,-1]The first two queries return 10 and -1. Updating key 2 changes its value to 25, and removing it makes the final query return -1.
Example 2
operations = ["remove","put","get","put","get","remove","get"]keys = [0,0,0,0,0,0,0]values = [0,0,0,7,0,0,0]return = [0,7,-1]Removing missing key 0 has no effect. The first insertion stores 0, the second insertion replaces it with 7, and the last query occurs after removal.
Constraints
1 <= operations.length <= 100000.operations.length == keys.length == values.length.- Every operation is
"put","get", or"remove". 0 <= keys[i] <= 1000000.- For every
putoperation,0 <= values[i] <= 1000000.