I'm new to python and I'm trying to write a function whose description is as follows: I have a list of integers. From this list I have to find the item with max frequency and print that. This seems pretty straight forward unless I have a limitation that the function must complete the execution within 10 sec and should consume memory < 512 MB. For shorter list length my function works fine but for a list of length 100000 it tanks. I'm not able to optimize the code. I have 2 implementations for the same:
Implementation #1
def returnMaxFrequency(ar):
freqList = []
for val in ar:
freq = ar.count(val)
freqList.append(freq)
return(max(freqList))
Implementation #2
def returnMaxFrequency(ar):
freqDict = {x:ar.count(x) for x in ar}
maxFreq = max(freqDict.values())
return maxFreq
Eg
if ar = [3 2 1 3]
o/p: 2
Using NumPy is not an option here. (Can't use external package)