icon picker
Go


🔍 Conceptual Difference

Table 11
Feature
HashSet
HashMap
Purpose
Store unique keys only
Store key-value pairs
Key Focus
Presence/absence of keys
Association between key and value
Value Storage
Does not store values
Stores values mapped to keys
Use Case
Fast lookup to check membership
Fast lookup, insert, update key-value
There are no rows in this table

✅ Built-in Go Equivalents

In Go, while there's no native Set type, you can simulate a HashSet using a map[T]struct{} or map[T]bool.

✅ HashSet in Go (Simulated)

go
CopyEdit
set := make(map[int]struct{}) // or map[int]bool

// Add
set[5] = struct{}{}

// Check
if _, ok := set[5]; ok {
fmt.Println("Exists")
}

// Remove
delete(set, 5)

Here, struct{} is an empty struct that takes 0 bytes. It's commonly used in Go to simulate sets efficiently.

✅ HashMap in Go

go
CopyEdit
mp := make(map[int]string)

// Add or Update
mp[1] = "one"

// Get
val, ok := mp[1]
if ok {
fmt.Println("Found:", val)
}

// Remove
delete(mp, 1)

🧠 When to Use What?

Use a HashSet (i.e., map[T]struct{}) when:
You only care if an item is present or not.
You don't need to store associated values.
Example: Keeping track of visited nodes in a graph.
Use a HashMap (i.e., map[K]V) when:
You need to associate data with each key.
Example: Counting frequencies, caching results.

👨‍💻 Summary

HashSet = map[int]struct{} → only keys, no values
HashMap = map[int]string → key-value pairs

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.