B4.1.5 · Fundamentals of ADTs
Sets
A set answers exactly one question about membership: is this here or not? It never answers "how many times," and it never answers "in what order."
Explainer
Think of a mailing list that must send exactly one email per address, no matter how many times someone signs up. Storing addresses in a set makes duplicates impossible by construction: adding the same address twice just leaves the set exactly as it was.
A set is an unordered collection where each value can appear at most once. There's no index, no position, no "first element", only membership: a value is either in the set or it isn't. The core operation is the membership testA yes/no check: "is this value in the collection?" Nothing about position, count, or order, just present or not., asking whether a given value is present.
This is B4's own guiding question in miniature, which ADT is most appropriate for a given situation: a list and a set store data the same basic way, but the situation decides which one actually fits.
Real example: a class register, seat by seat. Order matters (seat 1 is always seat 1), and two students could share a name, duplicates are fine.
Advantage
- Keeps order and position (you can always ask for "the 3rd item")
Disadvantage
- Checking membership is slow (no shortcut: check every item, one by one, until found or the list ends)
Real example: the mailing list above. Position never matters, only "have I already got this address?", and duplicates are impossible by construction.
Advantage
- Duplicates are impossible (the add operation itself refuses to store the same value twice)
Disadvantage
- No position or order (there is no "3rd item" in a set; you can never ask for one)
Two different things this statement asks for. The syllabus asks you to construct a set as an ADT from first principles (a wrapper around a list, not Python's built-in set), covering four operations specifically: membership (is this value here?), add, remove, and subset/superset (is every element of one set also in the other?). It also expects you to use Python's real set operators (in, &, |, <=) when a question is written in that form. Both come up, and they're not the same skill.
What does "subset" mean? Set A is a subset of set B if every single element of A is also in B (B is then A's superset). In the diagram below, the overlap, {Beth, Chen}, is a subset of both circles: everyone in the overlap is, by definition, also in each original set. That's true of every intersection, not just this example.
Worked example
Two sets, and what's shared between them
subscribers holds everyone signed up. notified holds everyone already emailed. Click a name to see which set (or sets) it belongs to, or use the operator buttons below.
Click a name, or try an operator.
Click a name or an operator to see the result.
Click through: what add() and remove() actually do
Before reading a whole class definition, watch a set starting empty and five calls run on it in order, one at a time.
start
An empty set. Nothing has run yet.
Step 1 of 6
Watch steps 4 and 6 especially: adding a value that's already there changes nothing (that's what makes it a set, not a list), and removing a value that was never there is safe, not an error.
# Constructing all four required set operations from first principles class MySet: def __init__(self): self.items = [] def contains(self, value): # membership return value in self.items def add(self, value): if not self.contains(value): # the check that enforces uniqueness self.items.append(value) def remove(self, value): if self.contains(value): self.items.remove(value) def is_subset_of(self, other): for value in self.items: if not other.contains(value): return False # found one element NOT in other: not a subset return True # every element checked and found: it is a subset # Using Python's real set type and operators, a separate skill subscribers = {"Amir", "Beth", "Chen"} notified = {"Beth", "Chen", "Dara"} print("Beth" in subscribers) # membership test print(subscribers & notified) # intersection print(subscribers | notified) # union print({"Beth", "Chen"} <= subscribers) # subset test
// Constructing all four required set operations from first principles class MySet { List<String> items = new ArrayList<>(); boolean contains(String value) { // membership return items.contains(value); } void add(String value) { if (!contains(value)) { // the check that enforces uniqueness items.add(value); } } void remove(String value) { if (contains(value)) { items.remove(value); } } boolean isSubsetOf(MySet other) { for (String value : items) { if (!other.contains(value)) { return false; // found one element NOT in other: not a subset } } return true; // every element checked and found: it is a subset } } // Java's real Set type, a separate skill from building one by hand Set<String> subscribers = new HashSet<>(Arrays.asList("Amir", "Beth", "Chen")); Set<String> notified = new HashSet<>(Arrays.asList("Beth", "Chen", "Dara")); System.out.println(subscribers.contains("Beth")); // membership test System.out.println(subscribers.containsAll(notified)); // superset test
The guide's current generator only produces Python. Java is shown here as a reference for schools teaching that track. is_subset_of checks every element of one set against the other, one at a time; the built-in <= operator does the same check under the hood.
Tracing is_subset_of: a true case and a false case
The fourth required operation, traced the same way add and remove were above. subscribers is still {"Amir", "Beth", "Chen"}.
The false case doesn't need to check every element, one missing value is enough to return False straight away. The true case is the only one that has to check all the way to the end, since it can't rule anything out until it's checked everything.
Check your understanding
Given the sets above, what does print(subscribers & notified) output?
Common mistake
B4.1.5RecurringMarkers reported candidates losing marks on "describe this operation" questions by writing only what the operation is for (its purpose) instead of what it actually does, step by step. The two are easy to conflate, but a mark scheme almost always wants the mechanism, not the motivation.
What a weak answer looks like: "The add method is used to put a new value into the set." States the purpose, not the mechanism, no mention of the uniqueness check.
What it should say: "The add method checks whether the value already exists in the set. If it does not, the value is appended; if it does, nothing changes." Describes the actual steps.
Revision questions
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
[1]State the output of print(12 in {5, 12, 40}).
[2]Outline how a set can be used to ensure that only one confirmation email is sent to each customer, even if they submit the form twice.
Marking points
评分要点
- Keep a set of the customer identifiers (or email addresses) that have already been sent a confirmation.
- Before sending, check whether the customer is in the set; if they are, do not send again.
- If they are not, send the email and add them to the set. Because a set stores no duplicates, a second submission has no additional effect.
- 维护一个集合,存放已经发送过确认的客户标识(或电子邮件地址)。
- 发送前,检查该客户是否在集合中;如果在,就不再发送。
- 如果不在,就发送邮件并把他们加入集合。因为集合不存重复项,第二次提交没有额外效果。
[4]Construct a method is_subset_of(self, other) for a set implemented as a wrapper around a Python list, without using the built-in set type or its <= operator.
Marking points
评分要点
- def is_subset_of(self, other): loop over every value in self's underlying list.
- For each value, check whether it is present in other's underlying list (a linear search, or Python's in on the list).
- If any value of self is not found in other, return False.
- If the loop completes with every value found, return True.
- An empty self should return True, since every element of it is trivially in other.
- def is_subset_of(self, other):,遍历 self 底层列表中的每一个值。
- 对每个值,检查它是否存在于 other 的底层列表中(线性查找,或对列表使用 Python 的 in)。
- 如果 self 的任何值在 other 中找不到,返回 False。
- 如果循环结束时每个值都找到了,返回 True。
- 空的 self 应返回 True,因为它的每个元素都平凡地在 other 中。
Flashcards
Active recall for this statement's key terms.
Keep this statement's practice with you
Free account: unlock every revision question and flashcard for B4.1.5, and every other IB CS 2027 statement.