I have a question for my understanding of the below code. I understand the difference between a normal dict and a defaultdict, but my question is that with the below code, when I replace defaultdict with just dict() or {}, I get a KeyValue error for 7 (the first number in the list), why is this?
Surely it shouldn't return an error when called, because 7 is in the list?? Thanks
nums = [7,6,7,12]
class Solution:
def singleNumber(self, nums):
hash_table = defaultdict(int)
for i in nums:
hash_table[i] += 1
print(hash_table)
for i in hash_table:
if hash_table[i] == 1:
return i
x = Solution()
print(x.singleNumber(nums))