Sets and tuples solve different jobs
Sets and tuples solve different jobs
A club register should avoid duplicate member names, while a quick calculation may return both a total and a count. A Set stores unique elements without a promised order. A tuple groups a fixed collection of values that can have different types and optional field labels.
What you’ll learn
- Choose unique membership or an ordered list deliberately.
- Read a fixed tuple’s named fields.
Sets and tuples solve different jobs in the example
Membership sets
A club register should avoid duplicate member names, while a quick calculation may return both a total and a count.
Membership setsShared member
Sets support union, intersection and subtraction for membership questions.
Shared memberFixed report tuple
The duplicate Diya does not create a second set element.
Fixed report tuple
How sets and tuples solve different jobs works
Sets support union, intersection and subtraction for membership questions. Their elements must be Hashable. A tuple’s shape is fixed by its type; it is not a growable list. Labels such as total and count clarify what its fields mean without defining a full custom structure.
Work through sets and tuples solve different jobs
Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.
Swift · complete program
let art: Set<String> = ["Diya", "Saira", "Diya"]
let music: Set<String> = ["Saira", "Noor"]
print(art.intersection(music).sorted())
let report = (total: 160, count: 2)
print(report.total / report.count)Expected output
["Saira"]
80Follow the execution
The duplicate Diya does not create a second set element. The intersection contains Saira, and sorting gives deterministic output. The tuple carries two integers, so dividing its named fields returns the integer average 80.
Try this program in the Swift compiler. Paste the complete source above.
Practice: Sets and tuples solve different jobs
Your task: Which collection should preserve duplicate attendance entries in their original order?
Swift · complete program
let art: Set<String> = ["Diya", "Saira", "Diya"]
let music: Set<String> = ["Saira", "Noor"]
print(art.intersection(music).sorted())
let report = (total: 160, count: 2)
print(report.total / report.count)Worked answer
An array should. A set would discard duplicates and would not promise the original order, changing the meaning of the attendance record.
Match the collection to the question
A large tuple can become hard to understand; a named struct is often clearer for a domain model. Set membership describes equality under its element type, not proof that two equal names represent the same person.
Check your decision
Match the collection to the question
An array should. A set would discard duplicates and would not promise the original order, changing the meaning of the attendance record. A large tuple can become hard to understand; a named struct is often clearer for a domain model.
Further reading: official Swift language guide.
Reaching the recap and completing the quizzes are separate milestones.