简体   繁体   中英

Java - Most suitable data structure for finding the most frequent element

My program contains algorithms that output text (String). Eventually I want to print out the word that occurred the most. But before I do this, I need to store it in a data structure. So I was wondering what data structure is the best (easy and efficient) to store Strings and then be able to obtain the most frequent element? I don't want to use any libraries. Thanks

I don't think any data structure does exactly this but here is how I would do it.

Maintain a Map<String, Integer> of each word to the number of times it was encountered and as you update the map keep track of the string corresponding to the largest number stored. For example:

String maxWord = null;
Integer maxCount = -1;
Map<String, Integer> wordCount = new HashMap<String, Integer>();
for (String str : getMyProgramOutput()) {
  if (!wordCount.containsKey(str)) { wordCount.put(str, 0); }
  int count = wordCount.get(str) + 1;
  if (count > maxCount) {
    maxWord = str;
    maxCount = count;
  }
  wordCount.put(str, count);
}

Create a Map<String, Integer> . Every time you enter a String increment the Integer (you might have to create your own MutableInteger class. When you're finished search it (or keep a running count)

Why don't you just build a max heap where in each node will have the String and integer_occurrence . To get the most frequent word, get the root of the heap

you might want to consider using dictionary in DB. Because such data normally has to be persisted into physical media to prevent from losing after system reboot. In this case, dictionary is helpful. Only thing you need to do is to set up a dictionary table and other table(s) for storing information like frequency and positioning and etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM