Start learning free

LESSON 239 OF 671 · SETS

Guest Check-In in Go: Counting Missing Guests with a Set

Practice Read the full explanation

You get two slices 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 6
    Ctrl + Enter to check

    GO · SETS

    Guest Check In in Go, explained

    Problem overview

    guestsMissing gets two slices of names, invited and arrived, and returns how many invited guests have not arrived. A guest can check in more than once and still counts as one. Someone who wasn't invited can check in too, and that changes nothing.

    With invited = ["Ava", "Leo", "Mia", "Zoe"] and arrived = ["Leo", "Zoe"] the answer is 2: Ava and Mia are missing. With ["Ava", "Leo"] and ["Leo", "Max", "Leo"] the answer is 1: Leo checked in twice but is one guest, and Max wasn't invited. With nobody arrived, every invited guest is missing.

    Approach

    Start with everybody missing and cross names off as people arrive. A map used as a set suits that: it holds each name once, and delete removes a name without complaint even when it is not there. That one property covers both tricky cases. An uninvited guest isn't a key, so delete does nothing. A guest who checks in twice is removed the first time, and the second delete does nothing.

    What remains in the set at the end is exactly the invited guests who never checked in, so len of the set is the answer.

    Step by step

    1. Create the set missing := map[string]bool{}.
    2. Loop over invited and set missing[name] = true. The names in invited are all different, so each becomes its own key.
    3. Loop over arrived and call delete(missing, name).
    4. Return len(missing).

    The solution in Go

    func guestsMissing(invited []string, arrived []string) int {
        missing := map[string]bool{}
        for _, name := range invited {
            missing[name] = true
        }
        for _, name := range arrived {
            delete(missing, name)
        }
        return len(missing)
    }
    

    Each loop writes _, name because only the value of each slice element is needed, not its index. Go would refuse to compile an index variable that is never used.

    Tracing the second example makes the two tricky cases visible. The set starts with two names, so len(missing) is 2. After the first arrival, Leo, delete removes him and the length is 1. Max was never invited, so delete finds nothing and the length stays 1. The second Leo is already gone, so the length stays 1 again. The function returns 1, and only Ava is missing.

    The order of the two loops matters: the invited guests must go in first, otherwise there would be nothing to delete from. Also note that the answer comes from the set itself. It is not computed from the slice lengths, since the slices can include repeats and uninvited people.

    Testing

    Running the function on the examples gives:

    invited = [Ava Leo Mia Zoe], arrived = [Leo Zoe]      -> 2
    invited = [Ava Leo], arrived = [Leo Max Leo]          -> 1
    invited = [Sam Kim], arrived = []                     -> 2
    

    Another way to think about the same task is to build a set of the arrivals and count the invited names that are not in it. That also works, but it needs a test inside the counting loop. Removing names from a set of the invited guests needs no test at all, and the answer is just the size of what is left.

    Common mistakes

    • Counting the arrivals. len(arrived) counts repeats and uninvited guests. Only the invited names matter.
    • Subtracting lengths. len(invited) - len(arrived) goes wrong as soon as someone checks in twice or wasn't invited.
    • Checking whether the name is present before deleting. It isn't needed; delete on a missing key is safe.
    Keep going with Go500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free