Simple solution in python


  • -3
    Z
    class Solution:
    # @param A, a list of integer
    # @return an integer
    def singleNumber(self, A):
        map = {}
        for i in range(len(A)):
            map[A[i]] = map.get(A[i],0) + 1
        map_sorted = sorted(map.iteritems(), key=lambda d: d[1], reverse=True)
        return map_sorted[-1][0]

  • 0
    A

    Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?


  • -2
    C
    import collections
    
    class Solution:
        def singleNumber(self, nums):
            c = collections.Counter(nums)
            return filter(lambda v: v[1] % 3 != 0, c.iteritems())[0][0]

Log in to reply
 

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