#C5067. Merge User Entries

    ID: 48675 Type: Default 1000ms 256MiB

Merge User Entries

Merge User Entries

You are given a list of user entries in JSON format via standard input. Each user entry is a JSON object containing an "email" field (a string) and a "fields" field (a JSON object mapping string keys to string values). Duplicate user entries (i.e. multiple entries with the same email) should be merged.

When merging the entries of the same email, for every key in the "fields" objects, if the key exists in more than one entry, concatenate the corresponding values in the order they appear separated by a comma and a space. Formally, for each email and key \( k \), if the value appears in several entries as \( v_1, v_2, \ldots, v_n \), then the merged value is:

[ merged(k) = v_1,, v_2,, \ldots,, v_n ]

Finally, output the merged entries sorted by the email in lexicographical (ascending) order.

inputFormat

The input is given as a single JSON array via standard input. Each element of the array is a JSON object representing a user entry with the following format:

{
    "email": "user@example.com",
    "fields": { "key1": "value1", "key2": "value2", ... }
}

There is no separate line count; simply read the complete JSON array from stdin.

outputFormat

Output a JSON array of merged user entries (to stdout) where duplicate emails have been merged as described above. The resulting array must be sorted in ascending lexicographical order by the "email" field.

## sample
[{"email": "john.doe@example.com", "fields": {"first_name": "John", "last_name": "Doe"}}]
[{"email": "john.doe@example.com", "fields": {"first_name": "John", "last_name": "Doe"}}]