So, I was learning Data Structures in Java on my own. And there was a "challenge question" in the book. The question basically asked me to make a data structure that could store all the words in a novel, and then it would associate with the word, where it occurs (like line number and character count) and the frequency of the amount of times it occurs. I was wondering which data structure should I use to store all this while maintaining a relatively Fast look up time and such.

I was thinking either hashmaps or tries, but I don't know which trie to use and how to associate the frequency of the word and stuff

Thanks in advance!


1 Answer 1


You will probably end up using two nested data structures. I would use a hashmap with the words as keys. The value would be an array/list/set (depending on what you want to do with the occurrence data) containing an item for each occurrence of the word. The item would store whatever you needed (line number, etc). The number of occurrences would just be the size of this array/list/set.

  • $\begingroup$ Thank you for replying. So I should not go for a trie structure yes? $\endgroup$
    – Anindya
    Oct 17, 2018 at 2:13
  • $\begingroup$ I suppose you could, but for most strings, a hashmap would probably be faster. $\endgroup$
    – HackerBoss
    Oct 17, 2018 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.