'GFG problem Find common elements in three sorted arrays
For this problem my approach is : copy all the elements of all three arrays in vector. Then count the frequency of each element in the vector. Any element which have frequency equal to 3 ,will be printed.
MY CODE:
class Solution
{
public:
vector <int> commonElements (int A[], int B[], int C[], int n1,
int n2, int n3)
{
//code here.
vector<int>v;
unordered_map<int, int> mp;
//copying the elements of all the array in the vector
for(int i=0;i<n1;i++){
v.push_back(A[i]);
}
for(int i=0;i<n2;i++){
v.push_back(B[i]);
}
for(int i=0;i<n3;i++){
v.push_back(C[i]);
}
// Traverse through array elements and
// count frequencies
for (int i = 0; i < v.size(); i++)
mp[v[i]]++;
// Traverse through map and print frequencies
for (auto x : mp){
if(x.second==3){
cout<<x.first;
}
}
}
};
In this solution, I am getting a segmentation fault. But I am unable to understand why I am getting this error. And also can anyone please explain the meaning of line->
vector <int> commonElements (int A[], int B[], int C[], int n1,
int n2, int n3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
