class Solution:
def singleNumber(self, nums: List[int]) -> int:
counter = collections.Counter(nums)
for key,value in counter.items():
if value == 1:
return key
位运算 中等 每日一题 遍历检查就完事了
https://leetcode-cn.com/problems/single-number-ii/
或者也可以使用位运算的方法
class Solution:
def singleNumber(self, nums: List[int]) -> int:
a = b = 0
for num in nums:
a, b = (~a & b & num) | (a & ~b & ~num), ~a & (b ^ num)
return b