Problem overview
The method guestsMissing receives two arrays of names, invited and arrived, and returns how many invited guests have not arrived. A guest may check in more than once and still counts as one. Someone who was not invited may check in too, and that does not change the answer. The names in invited are all different.
For invited = ["Ava", "Leo", "Mia", "Zoe"] and arrived = ["Leo", "Zoe"] the answer is 2: Ava and Mia are still missing. For invited = ["Ava", "Leo"] and arrived = ["Leo", "Max", "Leo"] the answer is 1. Leo checked in twice but is one guest, and Max was never invited. With nobody arrived, the answer is the number of invited guests.
Approach: start with everyone missing
Subtracting the lengths of the arrays does not work. In the second example it would give 2 - 3 = -1, because repeats and uninvited guests inflate the arrivals.
Think of it the other way round instead. At the start, every invited guest is missing. Each arrival takes one name off that list. What remains at the end is exactly the guests who never showed up. A set suits this: it holds each name once, and removing a name that is not in it does nothing.
- A guest who checks in twice is removed the first time. The second removal finds nothing and changes nothing.
- An uninvited guest was never in the set, so removing them changes nothing either.
Step by step
- Build the set of missing guests from
invitedwithnew Set(invited), typed asSet<string>. - Loop over
arrivedwithfor...of, one name at a time. - Delete each name from the set. No
ifis needed, becausedeleteon a missing value is not an error. - Return
missing.sizeafter the loop.sizeis a property of a set, written without parentheses.
The solution in TypeScript
class Solution {
guestsMissing(invited: string[], arrived: string[]): number {
let missing: Set<string> = new Set(invited);
for (let name of arrived) {
missing.delete(name);
}
return missing.size;
}
}
Two details are worth knowing:
deletereturns a boolean. It givestruewhen it removed something andfalseotherwise. The solution ignores that value, because the set's final size is all it needs.- The input array is not changed.
new Set(invited)copies the names into a new set, so the deletions touch only the set. After the first example,invitedstill has its 4 names. - The set also tells you who is missing. Spreading it with
[...missing]after the loop givesAva Miafor the first example, which is handy when you want the names and not just their number.
Edge cases and common mistakes
- Nobody arrived. The loop does not run, and the answer is the number of invited guests.
- Counting arrivals. Counting the entries of
arrivedthat are ininvitedover-counts guests who check in twice. - Using
length. A set hassize, notlength.missing.lengthisundefined, and the compiler reports it as an error. - Checking first. Writing
if (missing.has(name))before deleting is not wrong, but it adds nothing.
Testing
Running the method on the lesson's examples gives:
["Ava", "Leo", "Mia", "Zoe"]and["Leo", "Zoe"]gives2.["Ava", "Leo"]and["Leo", "Max", "Leo"]gives1.["Sam", "Kim"]and[]gives2.
