Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Most efficient way to check if a String is in a wordlist

I have an array of strings String[] words and a 28000 word Word-list.

I want to check if any member of the String array is in the WordList (the word-list is in a text file wordlist.txt)

What is the most efficient way to go about this?

like image 878
Eduardo Avatar asked Dec 15 '22 06:12

Eduardo


1 Answers

Place the strings directly into a HashSet<String> rather than an array and iterate through the file using contains on the set to check the content. You wont improve on O(1) access. This will also mimimize memory used to store the Strings should any duplicates exist.

like image 135
Reimeus Avatar answered Mar 07 '23 21:03

Reimeus