I need help writing a function findMode(aList) that accepts a list of items as a parameter, and proceeds to find the mode. However, your Python solution must use a dictionary to keep track of the items and their counts as the method of finding the mode (not parallel lists).
Here is the code that i have already tried:
def findMode(aList): aList.sort() position = 0 largestCount = 0 listLength = len(aList) while(position <= listLength): count = aList.count(aList[position]) if(count > largestCount): largestCount = count valuePosition = aList[position] position += count return aList[valuePosition]
Here are some examples of the desired outcome:
>>> findMode([4,3,4,3,2,1,4,5,3,3,4,5,1,4,1,4,5])  >>> findMode(["horse ", "cat ", "dog ", "turtle ","horse ", "cat ", "dog ", "turtle "]) ['horse ', 'cat ', 'dog ', 'turtle ']