Intersection of Two Arrays
Practice this coding question on Coding Gurukul.
Problem statement
Two student clubs record the numbered activity kits they used during a festival. Their logs may list the same kit several times because it was checked out repeatedly. The equipment desk wants a shared-kit report containing only the kit numbers present in both clubs' logs, with each shared number listed once. Your task Given two integer arrays, find their distinct common values. Repeated occurrences in either array must not create repeated output values. For a deterministic report in this workspace, print the result in increasing order. If no value belongs to both arrays, print EMPTY. Values that appear in only one log are excluded. Worked example For [7, 2, 7, 5] and [5, 7, 7, 9], the shared kit numbers are 5 and 7. Print 5 7: the repeated 7 is included only once. For [1, 2] and [3], print EMPTY.
Input format
First line: n m. Second line: n integers for the first array. Third line: m integers for the second array.
Output format
Print distinct common values in increasing order, separated by spaces, or EMPTY if there are none.
Constraints
1 <= n, m <= 1000; 0 <= each value <= 1000.
Sample input
4 4 7 2 7 5 5 7 7 9
Sample output
5 7
Sign in to run or submit a solution in C++, Java or Python.