I'm trying to solve a problem where my program would read a log file containing IP addresses and UNIX timestamp. at any instant of time, I need to find the top K IP addresses with the highest number of hits over a time period of past X seconds. I see there exists an algorithm which is nothing but sorting the entire log history based on frequency count and extract top K elements from it . The complexity of this algorithm would be O(N + NlogN) where N is the number of log entries. I'd like to know if we can do better than this?