Start learning free

LESSON 238 OF 674 · SETS

Guest Check In in C#: Counting Missing Guests With a HashSet

Practice Read the full explanation

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

    C# · SETS

    Guest Check In in C#, explained

    Problem overview

    The method GuestsMissing gets two lists of names, invited and arrived. It returns how many invited guests haven't arrived yet.

    Two details matter. A guest may check in more than once, and still counts as one guest. And someone who wasn't invited may check in too, which doesn't change the answer.

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

    Approach

    Start by assuming that every invited guest is missing. Then go through the arrivals and cross each name off. Whatever is left at the end is the answer.

    A HashSet fits this well. It holds each name once, and its Remove method quietly does nothing when the name isn't in the set. That one behaviour handles both tricky cases: a guest who checks in twice is crossed off the first time and the second Remove finds nothing, and an uninvited guest was never in the set to begin with.

    Step by step

    1. Build the set of missing guests from invited: new HashSet<string>(invited).
    2. Loop over arrived with foreach, using a string named name.
    3. Remove each name from missing. No if is needed: a repeat or an uninvited name is simply ignored.
    4. Return missing.Count after the loop.

    The solution in C#

    class Solution {
        public int GuestsMissing(List<string> invited, List<string> arrived) {
            HashSet<string> missing = new HashSet<string>(invited);
            foreach (string name in arrived) {
                missing.Remove(name);
            }
            return missing.Count;
        }
    }
    

    The details worth knowing:

    • Remove returns a bool (true if it removed something). This solution ignores it, which is allowed in C#.
    • The size of a set is Count, not Length.
    • The method doesn't change the invited list: the set is a separate copy.

    Tracing the second example

    The set missing starts as Ava and Leo. The arrivals are then handled one by one:

    nameRemove returnsnames left
    Leotrue1
    Maxfalse1
    Leofalse1

    Max was never invited and the second Leo is already gone, so neither changes the set. One name, Ava, is left.

    Testing

    Run on the statement's three examples, the method returns:

    2
    1
    2
    

    The third example passes an empty arrived list, so the loop never runs and the set keeps both names.

    Edge cases and common mistakes

    • Subtracting list sizes. invited.Count - arrived.Count gives -1 for the second example, because it counts the repeated check-in and the uninvited guest. Only the set removal handles those cases.
    • Counting arrivals that are invited. That also works if you remember to count each guest once, but it needs extra care for repeats; crossing names off a set doesn't.
    • Checking Contains before Remove. It's harmless but unnecessary, because Remove of a missing value does nothing.
    • Using a list for missing. List.Remove also returns false for a missing value, but it removes only one copy and checks values one by one, so a set is the better fit.
    Keep going with C#500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free