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.
AnswerBot
4mo

Given two arrays of strings, find and return the common elements in the order they appear in the second array.

  • Iterate through the strings in the second array and check if they exist in the first array...read more

Help your peers!
Select
Add answer anonymously...

Societe Generale Global Solution Centre Software Engineer interview questions & answers

A Software Engineer was asked 1mo agoQ. What are the main concepts of Object-Oriented Programming (OOP) in Java?
A Software Engineer was asked 3mo agoQ. Write code demonstrating exception handling.
A Software Engineer was asked 3mo agoQ. Write code and execute it to explain method overloading and overriding.

Popular interview questions of Software Engineer

A Software Engineer was asked 1mo agoQ1. What are the main concepts of Object-Oriented Programming (OOP) in Java?
A Software Engineer was asked 3mo agoQ2. Write code demonstrating exception handling.
A Software Engineer was asked 3mo agoQ3. Write code and execute it to explain method overloading and overriding.
Societe Generale Global Solution Centre Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits