I have a quick question about the below code regarding hash tables. For line 5- what is happening? So we initialised 'hash_table' to be a dictionary. Then for each element 'i' in nums we do hash_table[i]?? But the hash_table is empty- since just initialised it? This is where I am confused. Is it correct to say that we are defining the keys by doing hash_table['i']? If this is the case, why +=1? (P.S. nums is a list of integers shall we say)
class Solution:
def singleNumber(self, nums: List[int]) -> int:
hash_table = defaultdict(int)
for i in nums:
hash_table[i] += 1
for i in hash_table:
if hash_table[i] == 1:
return i