Start learning free

LESSON 240 OF 678 · SETS

Guest Check-In in Swift: Counting Missing Guests 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

    SWIFT · SETS

    Guest Check In in Swift, explained

    Problem overview

    The function guestsMissing receives two arrays of names, invited and arrived, and returns how many invited guests haven't arrived yet. A guest may check in more than once and still counts as one guest. Someone who wasn't invited may check in too, and that changes nothing. 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. For invited = ["Sam", "Kim"] and an empty arrived, the answer is 2.

    Approach: start with everyone missing, cross names off

    Counting arrivals directly goes wrong, because a repeated check-in or an uninvited guest would inflate the count. Turn the problem around instead. At the start, every invited guest is missing. Each arrival takes one name off the list of missing guests. What is left at the end is the answer.

    A set fits this well. Building it from invited gives one entry per guest. Removing a name that is in the set shrinks it by one. Removing a name that isn't in it, such as an uninvited guest or a second check-in of someone already removed, does nothing. Both awkward cases are handled without any extra if.

    Step by step

    1. Build the set of missing guests with Set(invited), stored in a var named missing, because names will be removed.
    2. Loop over the arrivals with for name in arrived.
    3. Remove each name from missing. For an unknown name or a repeat, remove returns nil and the set stays as it was.
    4. Return missing.count after the loop. These are the invited guests who never checked in.

    The solution in Swift

    class Solution {
        func guestsMissing(_ invited: [String], _ arrived: [String]) -> Int {
            var missing = Set(invited)
            for name in arrived {
                missing.remove(name)
            }
            return missing.count
        }
    }
    

    Two Swift details. First, remove returns the removed value as an optional, but it is marked so that the compiler doesn't warn when you ignore the result. Second, the parameters are unlabelled (_), so the function is called as guestsMissing(invited, arrived).

    Watching the set shrink

    Take the second example. The set starts with 2 names, Ava and Leo. The arrivals are then processed one at a time:

    ArrivalNames left in the set
    Leo1
    Max1
    Leo1

    The first Leo removes a name. Max is not in the set, and the second Leo is already gone, so neither changes the size. The function returns 1, for Ava.

    Testing

    On the three examples the function returns:

    2
    1
    2
    

    Common mistakes

    • Returning invited.count - arrived.count. For the second example this gives 2 - 3 = -1. Repeats and uninvited guests break the subtraction.
    • Counting arrivals that are in invited. Without a set, Leo's second check-in would be counted again. The set removes a name only once.
    • Using let for missing. Removing from a constant set is a compile error.
    • Worrying about an empty arrived. The loop just doesn't run, and the answer is the number of invited guests.
    Keep going with Swift500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free