Hash Tables

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!

--

--

--

Living in The Milky Way 😀

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aaron Ma

Aaron Ma

Living in The Milky Way 😀

More from Medium

Stack Data Structures: A brief overview

A stack of pancakes

Quick Sort — Javascript implementation

DSA #4 Binary Tree and Binary Search Tree

How to solve the Maximum Score from Performing Multiplication Operations Problem