Common elements | Practice | GeeksforGeeks

Given three arrays sorted in increasing order. Find the elements that are common in all three arrays.
Note: can you take care of the duplicates without using any additional Data Structure?
Example 1:
Input:
n1 = 6; A = {1, 5, 10, 20, 40, 80}
n2 = 5; B = {6, 7, 20, 80, 100}
n3 = 8; C = {3, 4, 15, 20, 30, 70, 80, 120}
Output: 20 80
Explanation: 20 and 80 are the only
common elements in A, B and C.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function commonElements() which take the 3 arrays A[], B[], C[] and their respective sizes n1, n2 and n3 as inputs and returns an array containing the common element present in all the 3 arrays in sorted order. 
If there are no such elements return an empty array. In this case the output will be printed as -1.
 
Expected Time Complexity: O(n1 + n2 + n3)
Expected Auxiliary Space: O(n1 + n2 + n3)
 
Constraints:
1 <= n1, n2, n3 <= 10^5
The array elements can be both positive or negative integers. 
0
shivkv047in 7 hours
vector <int> commonElements (int A[], int B[], int C[], int n1, int n2, int n3)
        {
            //code here.
            
            vector<int>ans;
            map<int,int>m;
            
            
             m[A[0]]++;
            
            for(int i=1;i<n1;i++){
                
                if(A[i] == A[i-1]) continue;
                
                m[A[i]]++;
                
            } 
            
             m[B[0]]++;
            
            for(int i=1;i<n2;i++){
                
                if(B[i] == B[i-1]) continue;
                
                m[B[i]]++;
                
            } 
            
            
             m[C[0]]++;
            
            for(int i=1;i<n3;i++){
                
                if(C[i] == C[i-1]) continue;
                
                m[C[i]]++;
                
            } 
            
            
            for(auto it:m){
                
                if(it.second >=3) ans.push_back(it.first);
            }
            
              return ans;

        }
0
anilcangulkaya716 hours ago
0.21/1.47
struct Array{   int* arr; int size;     int operator[](int index) { return arr[index]; }   Array() : arr(nullptr), size(0){};    Array(int* _arr, int _size) : arr(_arr), size(_size) { }};
static int QQSort(const void* a, const void* b){   return ((const Array*)a)->size > ((const Array*)b)->size;  }
vector<int> commonElements (int A[], int B[], int C[], int n1, int n2, int n3){   vector<int> result;      std::array<Array, 3> arrays =    {       Array(A, n1), Array(B, n2), Array(C, n3)      };      qsort(arrays.data(), 3, sizeof(Array), QQSort);
   int index0 = 0; int index1 = 0; int index2 = 0;      while(index0 < arrays[0].size)   {       int value = arrays[0][index0];              while(value > arrays[1][index1]) { index1++; }              while(value > arrays[2][index2]) { index2++; }              if (value == arrays[1][index1] && value == arrays[2][index2] && index1 < arrays[1].size && index2 < arrays[2].size )       {           if(!(result.size() > 0 && result[result.size()-1] == value))           result.push_back(value);           index1++;           index2++;       }              index0++;   }   return result;}
0
varshaagrawal0453 days ago
this code execution resulting runtime error / more time .
can anyone pls help , what is the optimized solution for this same approach?
 vector <int> commonElements (int A[], int B[], int C[], int n1, int n2, int n3)
        {
            //code here.
            vector<int> ans;
            int i =0, j =0, k =0;
            int ele = INT_MIN;
            while(i<n1 && j <n2 && k<n3){
                
                if(A[i]==B[j] && B[j]==C[k] && A[i]!=ele){
                ans.push_back(A[i]);
                ele = A[i];
                i++ , j++, k++ ;
               }
               
               else if (A[i]<B[j] ){
                   i++;
               }
               
               else if (B[j]<C[k] ){
                   j++;
               }
               
               else 
                   k++;
               
               
            }
            return ans;
        }
0
gkumars2956
This comment was deleted.
0
mahiratnaparkhe40521 week ago
Easy Peasy !
class Solution:   def commonElements (self,A, B, C, n1, n2, n3):       # your code here       X=list(set(A) & set(B))       Y=list(set(X) & set(C))       return sorted(Y)
0
0auo4ibkqq78lbk53gczafvjajc7o6a14edb6s2p1 week ago
vector<int>x;           int i=0,j=0,k=0;           while(i<n1){               int temp=min(B[j],C[k]);               if(A[i]<temp){                   i++;               }               else if((A[i]==B[j])&&(B[j]==C[k])){                   if(x.size()==0){                       x.push_back(A[i]);                   }                   if(A[i]!=x[x.size()-1]){                       x.push_back(A[i]);                   }                   i++;                   j++;                   k++;               }               else{                   char ch=B[j]<C[k]?'j':'k';                   if(ch=='j'){                       j++;                   }                   else{                       k++;                   }               }           }           return x;
 
/* It only passed 13 test cases, can someone tell me where i gone wrong*/
0
janvijindal051 week ago
int i=0,j=0,k=0;          vector<int> ans;          while(i<n1 && j<n2 && k<n3){              if(A[i]==B[j] && B[j]==C[k]){                  ans.push_back(A[i]);                  i++;                  j++;                  k++;              }                else if(A[i]<B[j]){                   i++;               }               else if(B[j]<C[k]){                   j++;               }               else{                   k++;               }               int prev1=A[i-1];               while(A[i]==prev1)i++;                              int prev2=B[j-1];               while(B[j]==prev2)j++;                              int prev3=C[k-1];               while(C[k]==prev3)k++;                             }           if(ans.size()==0) return {-1};              return ans;       } 
+2
radhesh1851 week ago
Extended Two Pointer approach in C++
vector <int> commonElements (int A[], int B[], int C[], int n1, int n2, int n3)
        {
            //code here.
            vector<int>res;
            int i=0,j=0,k=0,ele=INT_MIN;
            
            while(i<n1 && j<n2 && k<n3)
            {
                if(A[i]==B[j] && B[j]==C[k] && A[i]!=ele)
                {
                    res.push_back(A[i]);
                    ele=A[i];
                    i++;
                    j++;
                    k++;
                }
                else if(A[i]<B[j])
                {
                    i++;
                }
                else if(B[j]<C[k])
                {
                    j++;
                }
                else{
                    k++;
                }
            }
            return res;
        }
+1
badgujarsachin831 week ago
 vector <int> commonElements (int A[], int B[], int C[], int n1, int n2, int n3)
        {
            //code here.
            map<int,bool> mp;
            vector<int> v;
            for(int i=0;i<n1;i++){
                mp[A[i]]=false;
            }
            for(int i=0;i<n2;i++){
                if(mp.find(B[i])!=mp.end()){
                    mp[B[i]]=true;
                }
            }
            for(int i=0;i<n3;i++){
                if(mp.find(C[i])!=mp.end()){
                    if(mp[C[i]]==true){
                        v.push_back(C[i]);
                        mp.erase(C[i]);
                    }
                }
            }
            return v;
        }

0
shuklaakshita101 week ago
vector <int> commonElements (int A[], int B[], int C[], int n1, int n2, int n3)       {           //code here.               int i=0,j=0,k=0,ele;           vector<int> D;           while(i<n1 && j<n2 && k<n3)           {               if(A[i] == B[j] && B[j] == C[k])               {                  D.push_back(A[i]);                  ele=A[i];                   while(i<n1 && ele== A[i])                   i++;                   while(j<n2 && ele== A[i])                   j++;                   while(k<n3 && ele== A[i])                   k++;               }                else if (A[i] < B[j])                i++;                else if(B[j] < C[k])                j++;                else                k++;           }       return D;   }        
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.