TypeScriptMediumCollections25 min

Group typed records by category

Group records by category into objects {category,ids}. Preserve first-seen category order and input id order within each group, including duplicate ids. Inputs are valid JSON and must not be modified unless the algorithm uses a local copy. This exercise probes typed data modeling, boundary handling, and algorithm selection.

Input

One JSON value: Item[]. See the sample for its exact shape.

Output

One compact JSON value of type Group[]. Strings include JSON quotes; arrays preserve the specified order.

Constraints

Array/string lengths are 0 to 10000 unless stated otherwise. Numbers are integers in [-1000000,1000000]; all computed results fit the safe integer range.

Example

Input

[{"id":1,"category":"qa"},{"id":2,"category":"dev"},{"id":3,"category":"qa"}]

Output

[{"category":"qa","ids":[1,3]},{"category":"dev","ids":[2]}]

Example 2 input

[{"id":4,"category":"b"},{"id":3,"category":"a"}]

Output

[{"category":"b","ids":[4]},{"category":"a","ids":[3]}]

Checks a boundary or contrasting case beyond the public examples.

Public test cases (2)

  • Input

    [{"id":1,"category":"qa"},{"id":2,"category":"dev"},{"id":3,"category":"qa"}]

    Expected output

    [{"category":"qa","ids":[1,3]},{"category":"dev","ids":[2]}]
  • Input

    []

    Expected output

    []
Hints (2)
  1. Start with the required output order and the empty-input behavior.
  2. Map each category to the index of its output group

Target complexity: time O(n) · space O(n)

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All TypeScript programs