Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99
My code:
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
tmp_dict={}
for i in range(len(nums)):
if nums[i] not in tmp_dict.keys():
tmp_dict[nums[i]]=1
else:
tmp_dict[nums[i]]+=1
for item in tmp_dict.keys():
if tmp_dict[item]!=3:
return item