Common Elements Between Array of Strings Problem Statement

Given two 1-dimensional arrays containing strings of lowercase alphabets, determine and return the elements that are common in both arrays, i.e., the strings that appear in both arrays. An element from one array can only map to one element in the other array.

Example:
Array 1 = {"ab", "dc", "ab", "ab"} Array 2 = {"dc", "ab", "ab"} Output: "dc ab ab"

Input:

The first line contains an integer 'T' representing the number of test cases.
For each test case:
The first line includes two integers, 'N' and 'M', representing the sizes of the two arrays.
The second line contains 'N' single-spaced strings corresponding to the first array.
The third line contains 'M' single-spaced strings corresponding to the second array.

Output:

Output the common elements of both arrays in the order they appear in the second array, as a single space-separated string for each test case. Each test case should be printed on a new line.

Example:

For the arrays: Array 1 = {"ab", "dc", "ab", "ab"} and Array 2 = {"dc", "ab", "ab"}, the output is: "dc ab ab"

Constraints:

  • 1 <= T <= 10
  • 1 <= N, M <= 10000
  • 1 <= S <= 10 (Length of the strings)
  • Strings consist of lowercase alphabets only.
  • Time limit: 1 sec

Note:

You are not required to print anything; simply return the common elements in the specified order.
Be the first one to answer
Add answer anonymously...
Societe Generale Global Solution Centre Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter