Finding the mode of a list using ONLY loops and creating lists in python
Question: I have been trying to figure out how to find the mode of a list **WITHOUT**
using sorting functions nor importing any modules for quite awhile now...
Here's what I have so far:

    
    
    d = {}
    def mode():
        for i in mode:
            if i in d:
                d[i] += 1
            else:
                d[i] = 1
        return
    

How do I find the output such as the following:

    
    
    print(mode([1, 5, 6, 1, 1, 2]))
    [1]
    print(mode([5, 6, 7, 8, 9]))
    [5, 6, 7, 8, 9]
    

Thanks a bunch!


Answer: You need to count the occurrences in your dict and extract the `max` based on
the value returning the list itself if there is no mode.

    
    
    def mode(l):
         d= {}
        for i in l:
            d.setdefault(i, 0)
            d[i] += 1
        mx = max(d,key=d.get)
        return d[mx] if d[mx] > 1 else l 
    

