hash tables

lots of ways to hold data

not all of those ways are actually efficient, as it turns out

if the books aren't sorted

O(n) linear time

if the books are sorted

O(log n) logarithmic time

look up a book instantly?

array

hash function

+

hash table

holds our data

decides where our data should live

hash functions create a mapping and assign data to an index in the

array

data

3

sets of pairs: (k, v)

k = index in the array

v = value that lives at that index

hash buckets

function bookHashing(bookTitle, hashTableSize) {
  // Remove any spaces from book title.
  var strippedBookTitle = bookTitle.replace(/\s/g, '')
  
  // Divide the length of the title by the hash table size.
  // Return the remainder.
  return strippedBookTitle.length % hashTableSize;
}

bookHashing("The Grapes of Wrath", 12);
// 4

bookHashing("The Sound and the Fury", 12);
// 6

collisions 💣

two elements inserted into the same hash bucket

no hash function will return an unique hash bucket value for every item 

the size of our dataset will usually be larger than the size of our hash table

uses the 1st letter of input data to generate index key

alphabetization: 26 buckets

phonebook, dictionary

equally distributed data amongst the hash buckets

hash functions should

- always return the same key

- avoid collisions

- be easy to compute 

- avoid clustering of data 

linked lists,

stacks,

queues,

arrays

hash tables

caching

object representation

associative arrays

Made with Slides.com