Swift Academy · Lesson

Sets — membership & operations

Use Swift Set for unique elements, fast membership checks, set algebra (union/intersection/subtracting/symmetricDifference), and relations (subset/superset/disjoint).

Lesson 2 of 38 steps

Sets — membership & operations is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 3 lessons in total.

Why Sets?

Goal: Master Set for unique, unordered values with fast membership checks. You'll use set algebra and relations to reason about data.

Create & contains

Create a Set, add/remove, and check contains; duplicates are ignored.

var fruits: Set<String> = ["apple", "banana"]
fruits.insert("banana")      // no duplicate added
fruits.insert("cherry")
fruits.remove("apple")
print("has banana?", fruits.contains("banana"))

Set algebra

Use union, intersection, subtracting, and symmetricDifference.

let evens: Set<Int> = [2,4,6,8]
let odds: Set<Int>  = [1,3,5,7]
print("union", evens.union(odds).sorted())
print("intersect", evens.intersection([2,3,4,5]).sorted())
print("subtract", evens.subtracting([2,8]).sorted())
print("symDiff", evens.symmetricDifference([4,10]).sorted())

Relations

Relations: isSubset, isSuperset, and isDisjoint.

let a: Set<Int> = [1,2]
let b: Set<Int> = [1,2,3,4]
print("a ⊆ b?", a.isSubset(of: b))
print("b ⊇ a?", b.isSuperset(of: a))
print("disjoint?", a.isDisjoint(with: [3,4]))

Iterate & sort

Sets are unordered; sort when you need a stable order (e.g., UI output).

let colors: Set<String> = ["red", "blue", "green"]
// Sets are unordered; sort for deterministic output
for c in colors.sorted() { print(c) }

Tips & perf

Tips:

  • Prefer Set for membership tests (often O(1)).
  • Elements must be Hashable (String, Int, many enums work out of the box).
  • Convert to Array or use sorted() when order matters.

Symmetric difference

Quick check: Which method returns items in either set but not in both?

Recap

Recap: Sets store unique items and shine at membership checks. Practice set algebra (union/intersection/subtracting/symmetricDifference) and relations (subset/superset/disjoint).

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Sets — membership & operations” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Sets — membership & operations”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 3 lessons in total.

What will I learn in “Sets — membership & operations”?

Use Swift Set for unique elements, fast membership checks, set algebra (union/intersection/subtracting/symmetricDifference), and relations (subset/superset/disjoint). You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “Sets — membership & operations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arrays, Dictionaries & Sets
  2. Sets — membership & operations
  3. Iteration patterns & mutability
← Back to Swift Academy