Algorithm:

  1. Insert key-value pairs.
  2. While inserting, the key is hashed into an array index where the pair is stored.
  3. While retrieving, the pair is found by hashing the key once again and verifying that the proposed entry has the key.

Rust implementation:

#[derive(Copy, Clone)]
struct Entry {
    key: usize,
    value: i32,
}

struct Set {
    entries: [Entry; 100],
}

impl Set {
    fn new() -> Self {
        Set { entries: [Entry { key: 0, value: 0 }; 100] }
    }

    fn hash(&self, key: usize) -> usize {
        let mut attempt = key % self.entries.len();

        // Handle collisions.
        while self.entries[attempt].key != 0 && self.entries[attempt].key != key {
            attempt += 1;
        }

        attempt
    }

    fn add(&mut self, key: usize, value: i32) {
        self.entries[self.hash(key)] = Entry {key, value};
    }

    fn get(&self, key: usize) -> Option<i32> {
        let entry = &self.entries[self.hash(key)];
        match entry.key {
            0 => None,
            _ => Some(entry.value)
        }
    }
}