#K91232. Find Common Elements in Two Arrays

    ID: 37930 Type: Default 1000ms 256MiB

Find Common Elements in Two Arrays

Find Common Elements in Two Arrays

Given two arrays A and B with sizes \(N\) and \(M\) respectively, your task is to find all elements that are present in both arrays. The elements in the output must appear in the order they first appear in A and should not contain any duplicates.

In other words, if an element appears multiple times in A but is present in B, then only the first occurrence of that element (from A) should be printed. If there are no common elements, output an empty line.

Note: The solution should read input from standard input (stdin) and write the result to standard output (stdout).

inputFormat

The input is given via standard input in the following format:

  1. The first line contains two integers \(N\) and \(M\) separated by a space representing the sizes of arrays A and B respectively.
  2. The second line contains \(N\) space-separated integers representing the elements of array A.
  3. The third line contains \(M\) space-separated integers representing the elements of array B.

outputFormat

Output the common elements in a single line separated by a single space. The order of the elements should match the order they appear in array A, and duplicates should be removed. If there are no common elements, output an empty line.

## sample
6 5
1 2 3 1 4 2
3 4 5 6
3 4