awelgorithms

Get the value for a key

Updated

Get hashes the key, walks that bucket, and returns the stored value if the key is present. It does not scan other keys. It does not search by value.

What to focus on

get(1) on {4:2, 1:8, 9:3} is 8. Presence is about the key 1, not about whether 8 appears as a value elsewhere.

Worked examples

hit

a = [4, 1, 9] as {4:2,1:8,9:3}, get 1
  1. key 1 present

Result: 8

only key

a = [7] as {7:3}, get 7
  1. the one entry

Result: 3

empty

a = [], get 4
  1. no keys

Result: miss

Try it

Write get(m, k). m is the hash map. k is a key that exists. Return the value stored for k. Do not search by value. a = [4, 1, 9] as {4:2,1:8,9:3}, get 1 → 8. a = [7] as {7:3}, get 7 → 3. a = [], get 4 → miss.

keys are integers. The key exists when you get.

LeetCode problems that use this skill