Skip to main content
1 of 3

It is possible to count mode from list in linear time O(n), by Using dictionary data structure. It took 0.23 seconds (list length 1000 000). I have Hp Elitebook i5 processor. I used Python language.


import pandas as pd
import time
import statistics
class Mode:
    def __init__(self):
        self.data=[]
        self.numbers={}
        self.mode1=None
        self.largest_count=0

    def read_data(self):
        # Ignore the header, read the rest of the file:
        df = pd.read_csv('1M_random_numbers.txt', header=None, sep=r'\s+')
        self.data=df[0].to_list()
    
        
        
              
       
    def count(self):
        #print('mode statistics function: ',statistics.mode(self.data))
        
        for x in self.data:
            if x not in self.numbers:
                self.numbers[x]=1
            else:
                self.numbers[x]+=1
            if self.numbers[x]>self.largest_count:
                self.largest_count=self.numbers[x]
                self.mode1=x
            
    def mode(self):
        return self.mode1

if __name__ == "__main__":
    m = Mode()
    m.read_data()
    start=time.time()
    m.count()
    end=time.time()
    print(m.mode()) # 4
   
    print('Time in seconds: ', round(end-start,2))