Skip to content

Instantly share code, notes, and snippets.

@scottfrazer
Created July 27, 2022 19:57
Show Gist options
  • Save scottfrazer/49ae041f95ff58d9700136577216d952 to your computer and use it in GitHub Desktop.
Save scottfrazer/49ae041f95ff58d9700136577216d952 to your computer and use it in GitHub Desktop.
package main
// Write a HashMap implementation where the get and set operations
// have a runtime of O(1)
import (
"fmt"
"hash/fnv"
)
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
type HashMap struct {
}
func NewHashMap() *HashMap {
}
// Return "" if key is not in map
func (hashmap *HashMap) Get(key string) string {
return ""
}
func (hashmap *HashMap) Set(key, value string) {
}
func main() {
hmap := NewHashMap()
hmap.Set("k1", "v1")
hmap.Set("k2", "v2")
hmap.Set("k2", "v2_new")
fmt.Printf("k1 -> %s\n", hmap.Get("k1"))
fmt.Printf("k2 -> %s\n", hmap.Get("k2"))
fmt.Printf("k3 -> %s\n", hmap.Get("k3"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment