Skip to content
Exercise · Final project · lesson 5 of 7

Exercise: merge sorted reports

A shop's two cash registers send sales reports. A report is an array of [id, qty] pairs: an item number and how many units were sold, in ascending order of id. Write a function function mergeSales(a, b) that returns the combined report in the same form: the items from a and b in ascending order of id, and for an item in both reports, one pair with the sum of the quantities.

  • mergeSales([[1, 2], [4, 1]], [[1, 3], [5, 2]]) → [[1, 5], [4, 1], [5, 2]]
  • mergeSales([[2, 7]], [[1, 1], [3, 4]]) → [[1, 1], [2, 7], [3, 4]]
  • mergeSales([], [[6, 2]]) → [[6, 2]]

Constraints: each report holds from 0 to 1000 pairs, the id values within one report are all different, and id and qty are integers from 1 to 100000; for two empty reports the function returns an empty array; a, b and the pairs in them stay unchanged after the call.

Do not add extra imports unless the task says so explicitly.

merge.js — javascript
javascript · 4 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way