Title Description
In an integer array, except for two numbers, all other numbers appear even numbers. Please write the program to find out the two numbers that appear only once.
thinking
Similar to the first occurrence of a unique character in that string, Baidu used count counting; in addition, Baidu found that it could also use the Counter method of the collection module to compose a dictionary of list values and corresponding numbers.
Method 1:
class Solution: # Return to [a, b], where AB is the two number that appears at a time. def FindNumsAppearOnce(self, array): # write code here targets = [] for num in array: if array.count(num)==1 and num not in targets: targets.append(num) return targets
Method two:
import collections class Solution: # Return to [a, b], where AB is the two number that appears at a time. def FindNumsAppearOnce(self, array): # write code here targets = [] dic = collections.Counter(array) for key,value in dic.items(): if value < 2: targets.append(key) return targets