Skip to content
Learn Netverks

Lesson

Step 15/36 42% through track

sets-tuples-swift

Sets and tuples

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Sets and tuples: the syntax, patterns, and safety habits you need before advancing in Swift.

Teams still ship Sets and tuples in Swift codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Sets and tuples in contexts like: iPhone/iPad/Mac apps, server-side Swift (niche), and Apple toolchain projects.

Write Swift in main.swift with print(), click Run on server—the dev runner swiftc compiles and runs the binary (requires Swift toolchain, typically macOS; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Sets store unique unordered elements; tuples group fixed numbers of values of possibly different types—lightweight alternatives to small structs.

Set operations

var tags: Set = ["swift", "ios", "swift"]
tags.insert("macos")
print(tags.count)  // 3 — duplicates ignored

let a: Set = [1, 2, 3]
let b: Set = [3, 4, 5]
print(a.union(b))

Tuples

let pair = (name: "Ada", score: 98)
print(pair.name)
let (n, s) = pair

Important interview questions and answers

  1. Q: Set vs Array?
    A: Set guarantees uniqueness and unordered membership tests in O(1) average; array preserves order and allows duplicates.
  2. Q: Tuple vs struct?
    A: Tuples are anonymous and best for small temporary groups; structs scale with methods, protocols, and clarity.

Self-check

  1. What happens if you insert a duplicate into a Set?
  2. How do you access named tuple elements?

Tip: Prefer structs over tuples when the grouped data grows beyond two or three fields.

Interview prep

Set vs Array?

Set enforces uniqueness and unordered membership; array preserves order.

Tuple vs struct?

Tuples for small anonymous groups; structs scale with methods and clarity.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Set vs Array when?
  • Named tuple fields?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump