Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
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
            
        

Solution: https://leetcode.com/problems/single-number-ii/