Merge shift hours
Two scheduling desks each export employee-hour rows. Add hours by employee across both desks, including repeated names within one export, then print employee totals alphabetically. Keep zero-hour employees visible. Do not let the second export overwrite the first.
Input
First line A then A employee hours rows; next line B then B rows.
Output
Sorted employee total_hours lines.
Example 1
Input
2 ana 4 ben 2 2 ana 3 cy 1
Output
ana 7 ben 2 cy 1
Ana's four and three hours add to seven; Ben and Cy appear in only one export but remain in the combined alphabetical report.
Constraints
- Each export has 0 to 200 rows.
- Hour values are non-negative integers; employee names are single lowercase tokens.
Hints
Hint 1 of 2
Use one aggregation rule for both blocks.
Hint 2 of 2
?? 0 preserves a genuine stored zero.
Solution
Show a reference solution and explanation
const totals = new Map();
for (let block = 0; block < 2; block++) {
const n = Number(readline());
for (let i = 0; i < n; i++) {
const [name, hours] = readline().split(' ');
totals.set(name, (totals.get(name) ?? 0) + Number(hours));
}
}
for (const name of [...totals.keys()].sort()) console.log(name, totals.get(name));
Why it works
Both exports are inputs to one Map of totals. Add rather than replace on every row, so repetitions within or across desks are handled uniformly. Alphabetical sorting happens only after aggregation and includes employees whose total remains zero.
Lesson for this exercise: JavaScript Objects: Properties, Methods and Nesting