I need to print the K most-used words from a text file containing N words in O(N) time complexity.
I have tried it using HashMap by taking the word as key and number of occurrence as value and then sorting it by values. But sorting the HashMap by values takes O(NlogN) which is more than my requirement.
If K = 10 then I need to print the 10 most used words from a text file.