#C14043. Flatten Nested List

    ID: 43649 Type: Default 1000ms 256MiB

Flatten Nested List

Flatten Nested List

You are given a nested list which may contain elements of type integer or string, and the nesting can be arbitrarily deep. Your task is to flatten the list and output a flat list of unique elements, preserving their order of first occurrence.

In other words, if an element appears multiple times in the nested list, only its first occurrence (according to the depth‐first traversal) should be included in the output.

The input is provided as a single line in JSON format. You are required to output the flattened list in JSON array format.

Note: For any mathematical expression you may need, use the LaTeX format. For example, if an expression is needed, you could write \(a+b\) instead of a+b.

inputFormat

The input consists of a single line containing a JSON array that represents a nested list. Each element of the list can be either an integer, a string (enclosed in double quotes), or another list. For example:

[1, [2, [3, 4]], [1, [3, [5]]]]

outputFormat

Output a single line containing the flattened list in JSON array format. The output should contain only the unique elements in the order that they first appear in the nested list. For example:

[1, 2, 3, 4, 5]## sample

[1, [2, [3, 4]], [1, [3, [5]]]]
[1,2,3,4,5]