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
My program contains algorithms that output text (String). Eventually I want to print out
Share
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: