Data Structures and Algorithms

Hash Set vs Hash Map

What is the difference between a hash set and a hash map?

Abstract art representing computer programming

Hash sets and hash maps are both data structures that provide fast insertion, deletion, and search operations, but they serve different purposes and have some key differences:

Purpose:

  • Hash Set: A hash set is used to store a collection of unique elements. It provides a way to efficiently check the presence of an element and eliminate duplicates.
  • Hash Map: A hash map, also known as a hash table or dictionary, is used to store key-value pairs. It allows you to associate a unique key with a corresponding value and provides fast retrieval of values based on their keys.

Element Structure:

  • Hash Set: A hash set stores individual elements. Each element is unique, and there are no associated values or data attached to the elements.
  • Hash Map: A hash map stores key-value pairs. Each key is unique, and it is associated with a corresponding value. The keys are used to access and retrieve the associated values.

Accessing Elements:

  • Hash Set: In a hash set, you can check the presence of an element using the find() or count() member functions. You cannot directly access or modify elements in a hash set.
  • Hash Map: In a hash map, you can access and modify values using their associated keys. The [] operator or the at() member function can be used to retrieve or modify the value associated with a specific key.

Ordering:

  • Hash Set: The elements in a hash set are not stored in any particular order. The internal order of elements may change due to hash table rehashing or insertions and deletions.
  • Hash Map: Like hash sets, the key-value pairs in a hash map are not stored in any particular order. The internal order may change due to hash table rehashing or insertions and deletions.

Here's an example that demonstrates the usage of a hash set and a hash map:

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>

int main() {
  // Hash Set
  std::unordered_set<std::string> uniqueWords;
  uniqueWords.insert("apple");
  uniqueWords.insert("banana");

  // Duplicate, will be ignored
  uniqueWords.insert("apple");

  std::cout << "Hash Set:" << std::endl;
  for (const auto& word : uniqueWords) {
    std::cout << word << std::endl;
  }

  // Hash Map
  std::unordered_map<std::string, int> wordCounts;
  wordCounts["apple"] = 3;
  wordCounts["banana"] = 2;
  wordCounts["orange"] = 1;

  std::cout << "\nHash Map:" << std::endl;
  for (const auto& pair : wordCounts) {
    std::cout << pair.first << ": "
      << pair.second << std::endl;
  }
}
Hash Set:
apple
banana

Hash Map:
orange: 1
apple: 3
banana: 2

In this example, the hash set uniqueWords stores unique words, ignoring duplicates. The hash map wordCounts associates words with their counts, allowing easy retrieval and modification of the counts using the words as keys.

Hash sets are useful when you need to efficiently check the presence of elements and eliminate duplicates, while hash maps are useful when you need to associate values with unique keys and perform fast retrieval and modification of those values based on the keys.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved