Count Elements With Maximum Frequency
Real-world version: Peak Locker TallyThe statement here is our own: the same inputs, outputs and technique.
A lost-property desk writes one ledger line for every item handed in, and each line records the number of the locker the item was stowed in. lockers[i] is the locker number written on line i, and the same locker number can appear on any number of lines.
Work out the largest number of lines any single locker number holds. Then add up the lines belonging to every locker number that holds exactly that many lines, and return that total.
Examples
Example 1
- Input
- lockers = [45, 78, 45, 91, 78, 12]
- Output
- 4
Lockers 45 and 78 hold two lines each, while 91 and 12 hold one each, so two lockers tie at the top and they account for four lines between them.
Example 2
- Input
- lockers = [63, 27, 84, 19]
- Output
- 4
Every locker number holds a single line, so all four numbers tie at the top and all four lines are counted.
Example 3
- Input
- lockers = [56, 56, 56, 31, 74, 31]
- Output
- 3
Locker 56 holds three lines and no other number holds three, so only its three lines are counted.
Constraints
- 1 <= lockers.length <= 100
- 1 <= lockers[i] <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def peak_locker_lines(lockers: list[int]) -> int:public int peakLockerLines(int[] lockers)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's