Accepted Python code using dictionary. O(n) time, beats 76%


  • 0
    G
    class Solution(object):
        def singleNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            dict={}
            res=[]
            for i in nums:
                if i not in dict:
                    dict[i]=1
                dict[i]+=1
            for x in dict.keys():
                if dict[x]==2:
                    res.append(x)
            return res
    

    PS: Get the idea, this can be further polished without effecting run time.


Log in to reply
 

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