Given an unordered list of words, what's quickest way to test whether a certain word is in that list?
I can't think of another way to do this other than just going through each element in the list and checking whether it's the word I want.
Given an unordered list of words, what's quickest way to test whether a certain word is in that list?
I can't think of another way to do this other than just going through each element in the list and checking whether it's the word I want.
If you are given an unordered list and asked to test whether just one item is in the list then a sequential check of each item in the list is the best you can do.
If you want to carry out multiple tests (but you do not know all the test items in advance) then it is worth the initial overhead of sorting the list, creating a hash table, or creating a search tree.
It really depends on what data structure you want to use. If you are using an array, one way is to sort it in $O(n\log(n))$ and then you can check whether an item exists using binary search in $O(\log(n))$. One other way is to use hash tables data structures like dictionaries in Python where you can answer such question in $O(1)$. Check Python Dictionaries or C++ Maps.