URL
Design HashSet - LeetCode
Code
https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202305/design_hashset/main.fsx
type MyHashSet =
{ Table: int list[] }
static member init() : MyHashSet = { Table = Array.init 1024 (fun _ -> [])}
static member add (key: int) (s: MyHashSet) : MyHashSet =
let index = (hash key) % 1024
let lst = s.Table.[index]
if List.contains key lst then
s
else
let lst' = key :: lst
s.Table.[index] <- lst'
s
static member remove (key: int) (s: MyHashSet) : MyHashSet =
let index = (hash key) % 1024
let lst = s.Table.[index]
if List.contains key lst then
let lst' = List.except [ key ] lst
s.Table.[index] <- lst'
s
else
s
static member contains (key: int) (s: MyHashSet) : bool =
let index = (hash key) % 1024
let lst = s.Table.[index]
List.contains key lst