Simple, easy to understand Python 1 liner

  • 2

    There's really nothing to this. It's just a list comprehension iterating over a set of key, vals in a dictionary. Counter creates a dictionary of counts which does all the work for you basically.

     def singleNumber(self, nums):
        :type nums: List[int]
        :rtype: List[int]
        from collections import Counter as mset
        return [key for key, val in mset(nums).items() if val == 1]

Log in to reply

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