Last active
August 3, 2020 11:58
-
-
Save UNISERVO1/f4cd5fb730aedf9bb88b6098e893092e to your computer and use it in GitHub Desktop.
August LeetCoding Challenge Week 1: Day 2
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
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