Easiest Way to Sort a List of Words by Occurance

前端 未结 6 2086
南方客
南方客 2021-01-22 09:12

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

6条回答
  •  自闭症患者
    2021-01-22 10:07

    Why all so complicated? You need basically the following:

    1. Sort the words in-place. The same words will be grouped now.
    2. Go through the array, counting duplicates and store the resulting pairs (word, number of occurrences) in other array
    3. Sort the other array by number of occurrences.

    The complexity is O(n log n).

提交回复
热议问题