def mode(collection): # build dictionary from element -> number of occurrences count = {} for v in collection: count[v] = 1 + count.get(v, 0) # loop through results and keep track of item with greatest number mode = None bigCount = 0 for item,num in count.items(): if num >= bigCount: mode = item bigCount = num return mode