What is the best/easiest way to sort a large list of words (10,000-20,000) by the number of times they occur in the list, in Java. I tried a basic implementation but I get an ou
Build a HashMap
This will be much faster since you will only have to iterate over the list of words once. It's the difference between O(n) and O(n2), which for a large dictionary will be a tremendous difference.
At the end you can then take the list of words and sort them by count. You'll have to take them out of the map and add them to a separate data structure to do this. (Hint: you could use a TreeSet with a custom Comparator which compares words based on their frequency. Or, less elegantly, add them to a List
and then sort that list, again with a custom Comparator
.)