We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
We need to find frequencies of elements of both arrays, and store somewhere like element1:frequency, element2:frequency i.e. dictionary or what we call Hash Map in Data Structures. -> This is O(n) operation
For every unique element in brr array, we need to check if its frequency count is greater than how many times it appear in arr array. Since dictionary has unique elements as well as freq counts, lets loop over its keys, and compare values with arr's freq count (dictionary values). If greater add it in result list. -> O(n) to loop, comparisons on dict are O(1)
sort the result list and return it. -> O(klogk) where k is unique missing elements which will be <= 100.
Overall time complexity is around O(n).
defmissingNumbers(arr,brr):missing=[]arrcount=dict()# calculate and store frequency counts for every arr elementforainarr:ifarrcount.get(a):arrcount[a]+=1else:arrcount[a]=1brrcount=dict()forbinbrr:ifbrrcount.get(b):brrcount[b]+=1else:brrcount[b]=1forkeyinbrrcount:ifbrrcount[key]>arrcount.get(key,0):missing.append(key)returnsorted(missing)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Missing Numbers
You are viewing a single comment's thread. Return to all comments →
Python 3 Solution