0Pricing
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).

Sets — membership & operations is a free Swift Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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).

Frequently asked questions

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

Yes — the full text of “Sets — membership & operations” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

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