Chapter 65: Swift Sets

1. The most important intuition: What problem does a Set solve?

A Set is a collection that guarantees only unique values — no duplicates allowed — and gives you very fast “does this contain X?” answers.

Think of it as:

  • A guest list for a party where nobody can enter twice
  • A collection of tags on a blog post (you never want “swift, swift, ios”)
  • A set of permissions a user has (“read”, “write”, “delete” — no duplicates)
  • A collection of unique user IDs in a group chat

Key differences from Array:

Feature Array [T] Set <T>
Order Yes (keeps the order you add things) No — order is not guaranteed
Duplicates Allowed Not allowed — duplicates ignored
Fast “contains X?” Slow — must look through all items Very fast — uses hashing
Can I get element by index? Yes — array[3] No — no indices
Typical real question “What is the 5th item?” “Does this user have permission X?”

Golden rule you should memorize forever:

If order matters or duplicates are okay → use Array If uniqueness is required and you often ask “is X in the collection?” → use Set

2. Creating Sets – the most common ways

Swift

3. Most important Set operations (you will use these every day)

Swift

4. Real-life examples — code you will actually write

Example 1 – User permissions / roles (very common)

Swift

Example 2 – Unique tags / categories (blog, photo app, product filters)

Swift

Example 3 – Remove duplicates from user input

Swift

Example 4 – Check required permissions before action

Swift

5. Very Common Beginner Mistakes & Correct Habits

Mistake Wrong / Dangerous code Correct / Better habit Why?
Expecting order in Set print(mySet) assuming order Never rely on order in Set Sets are unordered (can change)
Using Set when order matters let steps = Set([“step1”, “step2”]) Use Array when order is important Set will lose order
Using Set when duplicates are allowed let cart = Set([“milk”, “bread”, “milk”]) Use Array for shopping cart Set removes duplicates
Forgetting Set is value type var a = b; a.insert(“x”) → b changes? No — a is copy, b unchanged Value type safety
Checking contains with Array instead of Set array.contains(“tag”) in hot path Convert to Set if you check contains often Array is O(n), Set is O(1)

6. Quick Summary – When to choose Set vs Array vs Dictionary

You want to… Choose this Typical real-life sentence Example real use-case
Keep order, allow duplicates Array “I have a list of things in order” shopping cart, messages, tasks, search results
Ensure uniqueness, fast “contains?” Set “I have a group of unique items” tags, permissions, unique IDs, friends (no dupes)
Fast lookup by key Dictionary “I want to find value by name/ID” user profile, settings, JSON, config

7. Small Practice – Try these right now

  1. Create a Set of 6 tags (add some duplicates on purpose) → Print how many unique tags you have → Check if “Swift” is present → Add 2 more tags → Remove one tag
  2. Create array of 10 email addresses (some duplicates) → Convert to Set to get unique emails → Print how many unique emails were found
  3. Create a Set of user roles/permissions → Check if user has both “read” and “write” → Check if user has “admin” or “moderator”

Paste your code here if you want feedback, corrections, or more polished versions!

What would you like to explore next?

  • Set operations in depth (union, intersection, difference, symmetric difference)
  • Array slicing & memory behavior
  • Sorting arrays (simple & custom comparators)
  • Dictionaries in depth (typed keys, default values)
  • Collections in SwiftUI (List, ForEach, @State)
  • Or move to another topic (optionals, switch, functions…)

Just tell me — we’ll continue in the same clear, patient, detailed style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *