Group sold product names by date and return, for each date, the distinct products sold and their counts.
You are given a list of sales records. Each record contains a date, a product name, and a quantity sold. For every date, aggregate the records so that each product appears once with the total number of units sold on that date.
Return the result organized by date in increasing order. For each date, list the products in lexicographic order, along with the summed quantity for that product.
Input Format
- A collection of sales records.
- Each record contains:
date: a string representing the sale date.product: a string representing the product name.quantity: a positive integer.
The exact input container may vary by language, but the logical structure is the same.
Output Format
Return a grouped structure ordered by date. For each date, include the product names sold on that date and the total quantity for each product.
Constraints
- Dates can be compared as strings in chronological order when formatted consistently, such as
YYYY-MM-DD. - Multiple records may exist for the same date and product.
- Product names are case-sensitive unless stated otherwise.
- The total quantity for a product on a date fits in a standard 32-bit signed integer.
Example 1
Input
records = [["2023-01-01","apple",3],["2023-01-01","banana",2],["2023-01-01","apple",1],["2023-01-02","banana",4]]
Output
[["2023-01-01",[["apple",4],["banana",2]]],["2023-01-02",[["banana",4]]]]
Explanation
On 2023-01-01, apple was sold twice, so its quantities are added: 3 + 1 = 4. The records are grouped by date, and products are listed once per date with their total counts.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.