Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Find similarities between two arrays.
EASY
20 mins
119 upvotes
Sorting
Binary Search
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Sorting
-
-
Binary Search
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Find similarities between two arrays.

Contributed by
Ambuj verma
Easy
yellow-spark
0/40
Avg time to solve 20 mins
Success Rate 80 %
Share
119 upvotes

Problem Statement

You have been given two arrays/list ‘ARR1’ and ‘ARR2’ consisting of ‘N’ and ‘M’ integers respectively. Your task is to return the number of elements common to ‘ARR1’ and ‘ARR2’ and the number of elements in the union of ‘ARR1’ and ‘ARR2’.

Example:
Let’s assume ‘ARR1’ is [1,2,3,4,5] and ‘ARR2’ is [2,4,6,8]. Elements common to ‘ARR1’ and ‘ARR2’ are [2,4] as they occur in both ‘ARR1’ and ‘ARR2’. Therefore the number of elements common to ‘ARR1’ and ‘ARR2’ is 2. Union of ‘ARR1’ and ‘ARR2’ is [1,2,3,4,5,6,8]. Therefore the number of distinct elements in the union of ‘ARR1’ and ‘ARR2’ is 7. So, the answer will be 2 7.
Note:
1. ‘ARR1’ consists of distinct integers i.e no number occurs twice in array/list.

2. ‘ARR2’ consists of distinct integers i.e no number occurs twice in array/list.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
2 2
1 2
3 4  
2 1
2 3
3
Sample Output 1:
0 4
1 2

Sample Output 1 Explanation:

Test case 1:

‘ARR1’ is [1,2]
‘ARR2’ is [3,4] 
Elements common to ‘ARR1’ and ‘ARR2’ are []. Therefore the number of elements common to ‘arr’ and ‘brr’ is 0. 
The union of ‘ARR1’ and ‘ARR2’ is [1,2,3,4]. Therefore the number of distinct elements in the union of ‘ARR1’ and ‘ARR2’ is 4.

Therefore the answer is 0 4.

Test case 2:

‘ARR1’ is [2,3].
‘ARR2’ is [3] 
Elements common to ‘ARR1’ and ‘ARR2’ are [3]. Therefore the number of elements common to ‘ARR1’ and ‘ARR2’ is 1. 
The union of ‘ARR1’ and ‘ARR2’ is [2,3]. Therefore the number of distinct elements in the union of ‘ARR1’ and ‘ARR2’ is 2.

Therefore the answer is 1 2.
Sample Input 2:
2
5 3
1 2 3 4 5
4 6 2
1 2
1
3 2
Sample Output 2:
2 6
0 3
Reset Code
Full screen
Auto
copy-code
Console