#C14515. Intersection of Integer Arrays

    ID: 44173 Type: Default 1000ms 256MiB

Intersection of Integer Arrays

Intersection of Integer Arrays

You are given two arrays that may contain integers as well as non-integer elements. Your task is to compute the intersection of the two arrays by considering only integer elements. The intersection should preserve the order of appearance in the first array and include each common integer as many times as it appears in both arrays. Formally, for any integer a, if it appears c1 times in the first array and c2 times in the second array, then it should appear \(\min(c_{1},c_{2})\) times in the output.

Note: Any element that is enclosed in quotes (e.g. "a" or 'a') should be treated as a non-integer even if it represents a numeric value. Also, any token that does not strictly represent an integer (for example, 2.5) should be ignored.

inputFormat

The input consists of four lines:

  • The first line contains an integer n denoting the number of elements in the first array.
  • The second line contains n space-separated tokens representing the elements of the first array.
  • The third line contains an integer m denoting the number of elements in the second array.
  • The fourth line contains m space-separated tokens representing the elements of the second array.

Important: Tokens that start with a double quote (") or a single quote (') are considered non-integer even if they appear numeric.

outputFormat

Print a single line that contains the intersection of the two arrays as space-separated integers. The intersection must include each integer exactly \(\min(c_{first}, c_{second})\) times, where cfirst and csecond are its counts in the first and second arrays respectively. If there is no intersection, print an empty line.

## sample
4
1 2 2 3
4
2 2 3 4
2 2 3