Time Complexity:
Space Complexity:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
hashtable = dict()
for num in nums:
if num in hashtable:
del hashtable[num]
else:
hashtable[num] = 1
return list(hashtable.keys())[0]
Time Complexity:
Space Complexity:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
deduplicated = list(set(nums))
timedSum = 0
for num in deduplicated:
timedSum += 2* num
for num in nums:
timedSum -= num
return timedSum
a ⊕ 0 = a
a ⊕ a = 0
a ⊕ b ⊕ a = (a ⊕ a) ⊕ b = 0 ⊕ b = b
cf) XOR 연산은 결합, 교환 법칙이 성립한다.
Time Complexity:
Space Complexity:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
res = 0
for num in nums:
res ^= num
return res