O'Reilly logo

A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7Blazing Fast Lookup with Hash Tables

Imagine that you’re writing a program that allows customers to order food to go from a fast-food restaurant, and you’re implementing a menu of foods with their respective prices. You could, of course, use an array:

 menu = [ [​"french fries"​, 0.75], [​"hamburger"​, 2.5],
 [​"hot dog"​, 1.5], [​"soda"​, 0.6] ]

This array contains several subarrays, and each subarray contains two elements. The first element is a string representing the food on the menu, and the second element represents the price of that food.

As we learned in Chapter 2, Why Algorithms Matter, if this array were unordered, searching for the price of a given food would take O(N) steps since the computer would have to perform a linear ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required