xxxxxxxxxx
#Counting frequency in a list using dictionary get() method
#if word is not in freqMap, get() returns 1, thus adding an entry key to dict
freqMap = {}
wordList = ['one', 'two', 'two', 'three', 'three', 'three']
for word in wordList:
freqMap[word] = freqMap.get(word, 1)
print(freqMap)
# {'one': 1, 'two': 2, 'three': 3}
xxxxxxxxxx
def countFreqWithDict(arr):
visited = {}
for i in range(len(arr)):
key = arr[i]
if key not in visited:
visited[key] = 0
visited[key] += 1
for i in visited:
print(f'{i} : {visited[i]}')
# Time complexity: O(n)
# space complexity: O(n)