Skip to content

Instantly share code, notes, and snippets.

@UNISERVO1
Last active August 3, 2020 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UNISERVO1/f4cd5fb730aedf9bb88b6098e893092e to your computer and use it in GitHub Desktop.
Save UNISERVO1/f4cd5fb730aedf9bb88b6098e893092e to your computer and use it in GitHub Desktop.
August LeetCoding Challenge Week 1: Day 2
class MyHashSet() {
val numBuckets: Int = 97
val buckets = List<MutableSet<Int>>(numBuckets){ mutableSetOf<Int>() }
val hashFunc : (Int) -> Int = { key: Int -> key % numBuckets }
fun add(key: Int) {
buckets.get(hashFunc(key)).add(key)
}
fun remove(key: Int) {
buckets.get(hashFunc(key)).remove(key)
}
/** Returns true if this set contains the specified element */
fun contains(key: Int): Boolean {
return buckets.get(hashFunc(key)).contains(key)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment