SECTION 12 / 31

Keep duplicate dates in a second dictionary

1 min read
A repeated date sets duplicates[date]=True. A third person on the same date does not create a second copy of that key.
A repeated date sets duplicates[date]=True. A third person on the same date does not create a second copy of that key.
birthdays = {}
duplicates = {}
foreach X in scores {
    dob = normalizedDayMonth(X.DateOfBirth)
    if (isKey(birthdays,dob)) {
        duplicates[dob] = True
    }
    else {
        birthdays[dob] = True
    }
}

birthdays remembers every observed birthday; duplicates remembers only dates seen at least twice. Both are dictionaries of True flags. Repeated assignment duplicates[dob]=True leaves one entry for that date. It does not count occurrences or remember students.

For a constructed group of three people with the same birthday, duplicates still has one date key, but there are three unordered student pairs. A date group of size r contains r(r−1)/2 such pairs. Do not use the number of duplicate-date keys as the pair count.

Try it yourself

After one occurrence, is its date in duplicates?

Show explained answer

No. The second occurrence creates the duplicate-date flag.

Try it yourself

Three records share 1 Jan. How many duplicate-date keys and matching pairs?

Show explained answer

One duplicate-date key and three unordered pairs.

Try it yourself

Do True flags identify the students?

Show explained answer

No. Store IDs in the birthday values when you need to know who shares the date.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑