260. Single Number III
https://leetcode.com/problems/single-number-iii
Python
Defaultdict
from collections import defaultdict
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
counters = defaultdict(lambda: 0)
for num in nums:
counters[num] += 1
return [num for num, count in counters.items() if count < 2]
Set
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
memory = set()
for num in nums:
if num not in memory:
memory.add(num)
continue
memory.remove(num)
return memory.pop(), memory.pop()