You can use the AVL Tree , the premise would be that if you assumed that 1,000,000 elements were specified in your array, it would take 1,000,000 steps to go through this data structure. Using the AVL Tree will need to follow the O(Log (1,000,000)) steps, which are == 6 , are pretty neat. This would be a good approach if your data were dynamic, although you would have to optimize the inserts.
With the AVL tree, everything will be sorted, so you get O(Log N) time. Instead of looping through an array this way for N Steps :

You might have something like this:

Where he checks the root and sees that Char c is larger than the first Char in dog , and moves to the left. Essentially, 1/2 search time is reduced by each step, making step O(Log N) . You need to maintain a balanced tree height.
The good thing about AVL Tree is that your data is sorted all the time, since the tree needs to be balanced.
If the data doesn't change often and you don't need the sorted data, it's probably best to use a HashMap .
source share