What's the fastest python solution?


  • 0
    T

    Your solution, how long does it take, how much submissions does it beat?


  • 0
    G

    class Solution(object):
    def singleNumber(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    dict= {}

        for element in nums:
            if element in dict:
                dict[element] += 1
            else:
                dict[element] = 1
        
        for key in dict.keys():
            if dict[key] == 1:
                return key
    

    #Linear Time But uses extra-space


  • 0
    C

    one line:

     return reduce (lambda x,y: x^y, A)
    

    tried once, 52 ms. Some one got 40ms, would like to see the code.


  • 0
    J

    i think it's faster if, after determining element in dict, deleting that key. That means there is only one key in dict at the end, and you can just return the first key in dict.keys()


Log in to reply
 

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