Sets: Unique Values and Membership
Count each student once at attendance
A scanner records identifiers 007, 009 and 007 again. A unique attendance count should be two, while the event log should still contain three scans.
A set groups distinct values and supports membership checks. JavaScript Set ignores a repeated equal primitive value rather than adding a duplicate entry.
Separate events and membership
Explain why scan count differs from student count.
Update membership
Add, test and remove a student identifier.
Choose an identity
Use stable identifiers rather than assuming equal-looking objects are duplicates.
Three scans, two unique identifiers
First scan
Add identifier 007.
size 1Second scan
Add identifier 009.
size 2Repeat scan
007 is already present.
size stays 2
A set answers presence rather than position
new Set creates a collection; add inserts, has tests membership and delete removes. size reports its unique entry count. There is no ordinary array-style set[0] positional access.
JavaScript Set preserves insertion order for iteration, but uniqueness is its main useful contract here. Other languages may not promise the same iteration order. Use an array when repeated occurrences matter.
Before starting, review ordered lists and repeated entries. These examples use JavaScript; other languages can use different syntax and rules.
Deduplicate the attendance scans
The repeated identifier 007 is one primitive string value, so the set has two entries. has returns true for 009 and false for unscanned 011.
The scan array remains three entries long. Making a set does not erase the original event history.
JavaScript · local teaching example
const scans = ['007', '009', '007'];
const attended = new Set(scans);
console.log(scans.length);
console.log(attended.size);
console.log(attended.has('009'));
console.log(attended.has('011'));Expected output
3
2
true
falseEqual fields do not imply one set entry
Two separately created objects with the same ID have different identities in JavaScript, so this set contains two entries.
If unique students are the task, store normalized IDs in the set rather than fresh objects from every scan.
JavaScript · local teaching example
const first = { id: '007' };
const second = { id: '007' };
console.log(new Set([first, second]).size);
console.log(new Set([first.id, second.id]).size);Expected output
2
1Apply the rule to a small change
Begin with 007 and 009, add 011 twice, then remove 009. Predict size and whether 009 remains.
Worked answer
The duplicate add has no extra effect, and removing 009 leaves two IDs. has returns false for the removed identifier.
JavaScript · local teaching example
const attended = new Set(['007', '009']);
attended.add('011');
attended.add('011');
attended.delete('009');
console.log(attended.size);
console.log(attended.has('009'));Expected output
2
falseRecognize mistakes and limits
Normalize identifiers consistently: numeric 7, text “7” and text “007” are distinct representations, and a set does not choose a school policy for matching them.
A set is not automatically a distributed uniqueness guarantee. Concurrent scanners and persistent records need a shared data rule. Decide whether membership removal means departure, a scan correction or deletion of history.
For the language or tool rules used here, see JavaScript sets and maps.
Check the core idea
JavaScript · code for this question
const scans = ['007', '009', '007'];
const attended = new Set(scans);
console.log(scans.length);
console.log(attended.size);
console.log(attended.has('009'));
console.log(attended.has('011'));Apply the idea to a new situation
Define uniqueness before selecting a set
Sets model distinct membership. Keep event history separately when duplicates matter, and choose a stable equality representation for the task.
Connect this idea with key-value lookup and structure selection.
Reaching the recap and completing the quizzes are separate milestones.