| https://leetcode.com/problems/design-hashset/ | Easy |
|---|
class MyHashSet {
private val size = 1009
private val buckets = Array(size) { mutableListOf<Int>() }
private fun hash(key: Int) = key % size
fun add(key: Int) {
val index = hash(key)
if (key !in buckets[index]) {
buckets[index].add(key)
}
}
fun remove(key: Int) {
val index = hash(key)
buckets[index].remove(key)
}
fun contains(key: Int): Boolean {
val index = hash(key)
return key in buckets[index]
}
}