Design HashSet

Another simple problem with simple solution :)

import java.util.*

class MyHashSet() {

    /** Initialize your data structure here. */
    val container = Array<LinkedList<Int>>(16, {LinkedList()})

    fun add(key: Int) {
        val index = key.hashCode() % container.size
        val list = container[index]
        if(list.isEmpty() || !list.contains(key)) list.add(key)
    }

    fun remove(key: Int) {
        val index = key.hashCode() % container.size
        container[index].remove(key)
    }

    /** Returns true if this set contains the specified element */
    fun contains(key: Int): Boolean {
        val index = key.hashCode() % container.size
        return container[index].contains(key)
    }

}

/**
 * Your MyHashSet object will be instantiated and called as such:
 * var obj = MyHashSet()
 * obj.add(key)
 * obj.remove(key)
 * var param_3 = obj.contains(key)
 */
Runtime: 456 ms, faster than 14.29% of Kotlin online submissions for Design HashSet.
Memory Usage: 64.8 MB, less than 100.00% of Kotlin online submissions for Design HashSet.
comments powered by Disqus