#C13254. Nested Frequency Replacement

    ID: 42772 Type: Default 1000ms 256MiB

Nested Frequency Replacement

Nested Frequency Replacement

Given a nested list of integers in JSON format, your task is to replace each integer with its overall frequency in the entire nested list. The nested list may contain multiple levels and the frequency is defined as the number of times the integer appears in the entire nested list.

For a given integer \(x\), its frequency is computed as \(f(x) = \#\{y \in L : y = x\}\). For example, if the input is [[1,2],[1,[2,[3,4],3]],4], the integer 1 appears twice, 2 appears twice, 3 appears twice, and 4 appears twice. Thus, the output should replace every integer with the number 2, resulting in [[2,2],[2,[2,[2,2],2]],2].

You are required to read the input as a JSON array from stdin and print the resulting modified nested list to stdout in JSON format.

inputFormat

The input is provided via standard input and consists of a single line containing a JSON array. The array represents a nested list containing integers and/or other nested lists. For example:

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

outputFormat

The output should be printed to standard output. It must be the JSON array where each integer in the original nested list is replaced by its frequency within the entire nested list. For instance, the output corresponding to the above input would be:

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