#K50587. Find Common Elements with Frequencies

    ID: 28897 Type: Default 1000ms 256MiB

Find Common Elements with Frequencies

Find Common Elements with Frequencies

Given two lists of elements, find all elements that are common to both lists. Each element should appear in the result as many times as it occurs in both lists (i.e. the minimum number of times it occurs in either list). For example, if the first list is [1, 2, 2, 3, 4] and the second list is [2, 2, 3, 5], the common elements are [2, 2, 3].

This problem requires you to read input from stdin and output the result to stdout.

inputFormat

The input consists of four lines:
1. An integer n, representing the number of elements in the first list.
2. n space-separated elements (each element can be an integer or a string) forming the first list.
3. An integer m, representing the number of elements in the second list.
4. m space-separated elements forming the second list.

Note: Elements that look like integers should be treated as integers.

outputFormat

Print the common elements in a single line, separated by a single space. If there are no common elements, output an empty line.## sample

5
1 2 2 3 4
4
2 2 3 5
2 2 3