A set stores distinct items: each item can appear only once. This makes sets useful when duplicates would add noise or cause repeated work. section 10.5 section 10.7
Sets become especially useful in these situations:
- Track people already searched during breadth-first search, preventing repeated work and infinite loops. section 6.7
- Track processed nodes in Dijkstra’s algorithm so each node is processed at most once. section 9.5
- Represent states, station coverage, and the final station selection in a greedy set-covering example. section 10.5
Sets also support operations that compare or combine collections. A union combines both sets, an intersection keeps shared items, and a difference removes items found in another set. section 10.7
Removing duplicates and comparing collections
Converting [1, 2, 2, 3, 3, 3] to a set produces {1, 2, 3}. With fruits and vegetables, intersection finds the shared tomato, while difference finds items belonging to only one collection. section 10.5 section 10.7
Use the accompanying algorithm diagrams and code listings to follow how sets prevent repeated graph processing. section 6.7














