Simple Python Solution


  • -1
    P

    Python solution 120 ms
    Takes advantage of the hash function and the fact that the dict is iterable. Loops through dict and returns the key
    weird formatting wouldn't make my init function a code block on here. Just initializes a dict(dict_vals)

    def insert(self, val):
        """
        Inserts a value to the set. Returns true if the set did not already contain the specified element.
        :type val: int
        :rtype: bool
        """
        if val in self.dict_vals:
            return False
        else:
            self.dict_vals[val] = True
        return True
        
    
    def remove(self, val):
        """
        Removes a value from the set. Returns true if the set contained the specified element.
        :type val: int
        :rtype: bool
        """
        if val in self.dict_vals:
            del self.dict_vals[val]
            return True
        return False
    
    def getRandom(self):
        """
        Get a random element from the set.
        :rtype: int
        """
        for key in self.dict_vals:
            return key
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.