Start learning free

LESSON 239 OF 677 · SETS

Guest Check-In in Scala: Counting Missing Guests With a Set

Practice Read the full explanation

You get two ArrayBuffers 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

    SCALA · SETS

    Guest Check In in Scala, explained

    Problem overview

    The method guestsMissing gets two ArrayBuffer[String] lists, invited and arrived, and returns how many invited guests have not arrived yet. A guest may check in more than once and still counts as one guest. A person who was not invited may check in too, and that changes nothing.

    For invited = ["Ava", "Leo", "Mia", "Zoe"] and arrived = ["Leo", "Zoe"], Ava and Mia are still missing, so the answer is 2. 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 invited = ["Sam", "Kim"] and nobody arrived, the answer is 2.

    Approach

    Start by assuming everyone invited is missing, then cross off each name that arrives. The names left at the end are the missing guests. A set suits this because taking a name out of a set that does not contain it does nothing. Both awkward cases, a guest who checks in twice and a stranger, are then handled without any extra checks.

    Step by step

    1. Build a mutable set missing from invited. The names in invited are all different, so the set starts with every invited guest.
    2. Loop over the arrivals with for (name <- arrived).
    3. Inside the loop, remove the name with -=. A stranger is not in the set, and a guest seen twice is gone after the first time, so -= does nothing in both cases.
    4. After the loop, return missing.size.

    The solution in Scala

    class Solution {
        def guestsMissing(invited: ArrayBuffer[String], arrived: ArrayBuffer[String]): Int = {
            val missing = scala.collection.mutable.Set[String](invited.toSeq: _*)
            for (name <- arrived) {
                missing -= name
            }
            return missing.size
        }
    }
    

    The set is mutable, so -= changes it in place, and val missing is fine because the name always refers to the same set. invited.toSeq: _* passes the buffer's names to the set one by one.

    Why a set and not a buffer

    With an ArrayBuffer, -= removes only the first matching value, and a missing name would need its own check. A set holds each name once, so a repeated check-in is harmless, and a name that is not there is ignored. The loop body is a single line.

    Testing

    On the three examples, the method returns 2, 1 and 2. An empty arrived buffer is fine: the loop does not run, and the answer is the size of the invited list.

    Another way

    Scala can subtract one collection from another when both are sets:

    val missingCount = (invited.toSet -- arrived).size
    

    For invited = ["Ava", "Leo"] and arrived = ["Leo", "Max", "Leo"] this is 1, like the loop. The loop version shows the idea step by step, which is the point of this lesson.

    Edge cases and common mistakes

    • Subtracting the sizes. invited.size - arrived.size gives -1 for the second example, because it counts the repeated Leo and the stranger Max.
    • Checking the stranger first. There is no need for an if around -=; a name that is not in the set is skipped by itself.
    • Looping over the wrong list. The loop goes over the arrivals, and the set holds the invited names, not the other way round.
    • Returning the set instead of its size. The method returns an Int.
    Keep going with Scala500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free