I have an array of 109582 strings ordered alphabetically. My web application will be making a lot of rapid checks for whether a given string is contained in the array. Obviously I could make a wrapper class that is a hash table or binary tree, but is there any native JavaScript data structure that I can use instead?
A heap will only let you search quickly for the minimum element (find it in O(1) time, remove it in O(log n) time). If you design it the other way, it will let you find the maximum, but you don't get both. To search for arbitrary elements quickly (in O(log n) time), you'll want the binary search tree. Good answer.
What is the most efficient data structure for this? Looking at complexity analysis, Hashtables seem to be the most efficient for lookups.
JavaScript has primitive (built in) and non-primitive (not built in) data structures. Primitive data structures come by default with the programming language and you can implement them out of the box (like arrays and objects).
In computer science, a lookup table is a data structure, usually an array or associative array, often used to replace a runtime computation with a simpler array indexing operation.
Sure. Make an dictionary object
dict = {
string1: 1,
string2: 1,
etc
It's guaranteed to provide O(1) lookup time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With