Problem no 6: Find the union of 2 arrays.

 

Union of two arrays
Basic Accuracy: 47.56% Submissions: 8647 Points: 1

Given two arrays A and B of size N and M respectively. The task is to find union between these two arrays.

Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions, then only one occurrence of element should be printed in union.

Example 1:

Input:
5 3
1 2 3 4 5
1 2 3

Output: 
5

Explanation: 
1, 2, 3, 4 and 5 are the
elements which comes in the union set of both arrays. 
So count is 5.
  
 
 
Answer:
        int doUnion(int a[], int n, int b[], int m) 
 {
set<int> s;
for(int i=0;i<n;i++)
{
s.insert(a[i]);
}
for(int i=0;i<m;i++)
{
s.insert(b[i]);
}
return s.size();

}

Comments

Popular posts from this blog

Problem no 18(array):count the number of pairs in array whose sum is equal to the given number.

convert the integer number to its binary representation.

Tcs assessment problem: museum problem.