A set represents which distinct values are present. A map associates each key with a value. Choosing between them starts with the question your algorithm needs to answer.
Worked example
To detect whether an identifier has appeared before, maintain a set of seen identifiers. For each new identifier, test membership and then add it. To count appearances, maintain a map from identifier to count and increase the stored count on every visit. The first structure records presence, while the second records frequency.
What to check
Use an example with repeated values to check the intended behavior. A set removes duplicate membership, so its size cannot tell you how many total observations arrived. Hash-based membership usually has expected constant-time operations under suitable hashing assumptions. Ordered tree-based structures have different time bounds and preserve an ordering. Choose the guarantee that matters to your caller and include the extra storage in the complexity estimate.
Continue with the book
Explore the full treatment, worked examples, and exercises in Algorithms Made Simple.
Companion article for Algorithms Made Simple, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
