Intersection of Two Unsorted Arrays Problem Statement

Given two integer arrays ARR1 and ARR2 of sizes 'N' and 'M' respectively, find the intersection of these arrays. The intersection is defined as the set of elements common to both arrays. The result should maintain the order as they appear in ARR1.

Note:

The input arrays can contain duplicate elements, and the intersection result should preserve order based on ARR1.

Input:

The first line contains an integer 't', which denotes the number of test cases. The next lines describe the test cases:
- For each test case:
- First line: an integer 'N' (size of ARR1)
- Second line: 'N' space-separated integers (elements of ARR1)
- Third line: an integer 'M' (size of ARR2)
- Fourth line: 'M' space-separated integers (elements of ARR2)

Output:

For each test case, output the intersection elements in one line, separated by a single space. Print each result for a test case on a separate line.

Example:

Input:
1
5
1 2 2 3 4
4
2 2 4 6
Output:
2 2 4

Constraints:

  • 1 <= t <= 10^2
  • 0 <= N <= 10^5
  • 0 <= M <= 10^5
  • Time Limit: 1 sec
AnswerBot
4mo

Find the intersection of two unsorted arrays while maintaining the order of elements from the first array.

  • Iterate through the elements of the first array and check if they exist in the second array.

  • Us...read more

Help your peers!
Select
Add answer anonymously...

Practo Software Developer interview questions & answers

A Software Developer was asked Q. What are magic functions and autoloading in PHP?
A Software Developer was asked Q. Implement a queue using two stacks.
A Software Developer was asked Q. Given three arrays sorted in non-decreasing order, print all common elements in ...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. What are magic functions and autoloading in PHP?
A Software Developer was asked Q2. Implement a queue using two stacks.
A Software Developer was asked Q3. Given three arrays sorted in non-decreasing order, print all common elements in ...read more
Practo Software Developer 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