Hash Tables

Aaron M
2 min readOct 2, 2019
Data Structure

Welcome! Today, let’s talk about Hash Tables! A hash table organizes data so you can quickly lookup values for any given key.

Strengths:

  • Fast lookups. Lookups take O(1) time on average.
  • Flexible keys. Most data types can be used for keys, as long as they’re hashable.

Weaknesses:

  • Slow worst-case lookups. Sometimes take O(n).
  • Unordered. Noooo!
  • No cache. What? This takes a long time!
Time Complexity

Examples of Hash Map

# Python 3
sunHeat = {
'core': 3.5, # in million of Sun's heat
}
// C++
// In C++, it's called unordered_map.
unordered_map<string, int> sunHeat;
// In million of Sun's Heat
sunHeat.insert(make_pair("core", 3.5));
// More Insertions
// JavaScript// Hash map is called object in JavaScript
const sunHeat = {
'core': 3.5, // in million of Sun's heat
};
// Java
// In Java, it's called hash map.
HashMap<String, Integer> sunHeat = new HashMap<>();

sunHeat.put("core", 3.5); // In million of Sun's Heat
// Swift
// In Swift, Hash Map is called dictionaries.
var sunHeat: [String: Int] = [:]

sunHeat["core"] = 3.5 // in million of Sun's heat

Having trouble with Hash Map?

Think of a hash map as an array.

Thank you for reading! If you enjoyed this, please clap for it! And don’t forget to follow me on Twitter; https://twitter.com/aaronhma/ and go to my personal website: https://aaronhma.com/ and stay tuned for more Algorithm & Data Structure stories! Have a great day!

--

--

Aaron M

Planet Earth, The Milky Way, Local Group, Virgo Supercluster, Laniakea Supercluster, the Universe