Created
March 4, 2023 23:09
-
-
Save Git-I985/b39cb70e1120a3543d4aa8e7786d6f68 to your computer and use it in GitHub Desktop.
JS Hash table implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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