Skip to content

Instantly share code, notes, and snippets.

@Git-I985
Created March 4, 2023 23:09
Show Gist options
  • Save Git-I985/b39cb70e1120a3543d4aa8e7786d6f68 to your computer and use it in GitHub Desktop.
Save Git-I985/b39cb70e1120a3543d4aa8e7786d6f68 to your computer and use it in GitHub Desktop.
JS Hash table implementation
// @ts-check
/* eslint-disable no-param-reassign */
import crc32 from 'crc-32';
const collision = (map, key, hash) => map[hash] && key !== map[hash][0]
// BEGIN (write your solution here)
export const get = (map, key, defaultValue = null) => {
const hash = crc32.str(key)
return collision(map, key, hash) || !map[hash] ? defaultValue : map[hash][1]
}
export const set = (map, key, val) => {
const hash = crc32.str(key)
if(collision(map, key, hash)) return false
map[hash] = [key, val]
return true
}
export const make = () => []
// END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment