Start learning free

LESSON 239 OF 675 · SETS

Guest Check-In in TypeScript: Counting With a Set

Practice Read the full explanation

You get two arrays of names, invited and arrived. Return how many invited guests haven't arrived yet.

A guest may check in more than once. They still count as one guest.

Someone who wasn't invited may check in too. They don't change the answer.

 

Example 1:

Input: invited = ["Ava", "Leo", "Mia", "Zoe"], arrived = ["Leo", "Zoe"]
Output: 2
Explanation: Leo and Zoe are here.
Ava and Mia haven't arrived yet.

Example 2:

Input: invited = ["Ava", "Leo"], arrived = ["Leo", "Max", "Leo"]
Output: 1
Explanation: Leo checked in twice, but he is one guest.
Max wasn't invited.
Only Ava is missing.

Example 3:

Input: invited = ["Sam", "Kim"], arrived = []
Output: 2
Explanation: Nobody has arrived yet.

 

Constraints:

  • invited has 1 to 100 names.
  • arrived has 0 to 100 names.
  • The names in invited are all different.
  • Each name is one word of English letters, starting with a capital letter.
STEP 1 OF 4
    Ctrl + Enter to check

    TYPESCRIPT · SETS

    Guest Check In in TypeScript, explained

    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

    1. Build the set of missing guests from invited with new Set(invited), typed as Set<string>.
    2. Loop over arrived with for...of, one name at a time.
    3. Delete each name from the set. No if is needed, because delete on a missing value is not an error.
    4. Return missing.size after the loop. size is 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:

    • delete returns a boolean. It gives true when it removed something and false otherwise. 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, invited still has its 4 names.
    • The set also tells you who is missing. Spreading it with [...missing] after the loop gives Ava Mia for 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 arrived that are in invited over-counts guests who check in twice.
    • Using length. A set has size, not length. missing.length is undefined, 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"] gives 2.
    • ["Ava", "Leo"] and ["Leo", "Max", "Leo"] gives 1.
    • ["Sam", "Kim"] and [] gives 2.
    Keep going with TypeScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free