Common Elements | Practice | GeeksforGeeks

Given two lists V1 and V2 of sizes n and m respectively. Return the list of elements common to both the lists and return the list in sorted order. Duplicates may be there in the output list.
Example:
Input:
n = 5
v1[] = {3, 4, 2, 2, 4}
m = 4
v2[] = {3, 2, 2, 7}
Output:
2 2 3
Explanation:
The common elements in sorted order are {2 2 3}
Your Task:
This is a function problem. You need to complete the function common_element that takes both the lists as parameters and returns a list of common elements. 
Constraints:
1 ≤ n, m ≤ 105
1 ≤ Vi ≤ 105
0
mayank2021in 8 hours
C++ : 0.55/4.16 vector<int> common_element(vector<int>v1,vector<int>v2)   {       //may2021              unordered_map<int, int>mp;       vector<int>result;       int n1=v1.size(), n2=v2.size();              for(int i=0; i<n1 ; i++)         mp[v1[i]]++;                for(int i=0; i<n2 ; i++)       {           if(mp.find(v2[i])!=mp.end())            {                result.push_back(v2[i]);                mp[v2[i]]--;                                if(mp[v2[i]]==0)                  mp.erase(v2[i]);            }          }        sort(result.begin(), result.end());       return result;          }
0
aamitprasad6183 days ago
CPP
 
sort(v1.begin(),v1.end());
       sort(v2.begin(),v2.end());
       
       int n=v1.size();
       int m=v2.size();
       
       int i=0;
       int j=0;
       
       vector<int>ans;
       
       while(i<n && j<m){
           if(v1[i]==v2[j]){
               ans.push_back(v1[i]);
               v1[i]=INT_MIN;
               i++;
               j++;
           }
           else if(v1[i] < v2[j]){
               i++;
           }
           else{
               j++;
           }
       }
       
       return ans;
0
chaitankhilar1 week ago
why i need to optimize my code ?
 ArrayList<Integer> al=new ArrayList<>();       Integer [] h=new Integer[v2.size()];       h=v2.toArray(h);       for(int i=0;i<v2.size();i++){           if(v1.contains(h[i])==true){               al.add(h[i]);           }       }       Collections.sort(al);       return al;
0
harshjha51220012 weeks ago
vector<int> common_element(vector<int>v1,vector<int>v2)   {       // Your code here       unordered_map<int,int>ump;       vector<int>v;       for(auto i:v1) ump[i]++;       for(auto i:v2)       {           if(ump.find(i)!=ump.end())           {   if(ump[i]!=0)               {    v.push_back(i);                   ump[i]--;               }                          }       }       sort(v.begin() , v.end());
              return v;   }
0
jangleshubham132 weeks ago
C++ 0.55/4.16 - Bruteforce + Optimisation
 
// Method 1: Bruteforce. TC: O(M*N) SC: O(N)
    vector<int> common_element1(vector<int>v1,vector<int>v2) {
        vector<int> ans;
        int n = v1.size();
        int m = v2.size();
        for(int i=0; i<n; i++) {			//N*M
        	for(int j=0; j<m; j++) {
        		if(v1[i] == v2[j]) {
        			ans.push_back(v1[i]);
        			v1[i] = INT_MIN;
        			break;
        		}
        	}
        }

        sort(ans.begin(), ans.end());		//NlogN
        return ans;
    }


	// Method 2: Sorting both array. TC: O(NlogN) Sc: O(N)
	// 	- Sort both the arrays
	// 	- Two pointers while(a<n && b<m)
	// 	- if match add to ans or else if v1[a] < v2[b] a++; else b++;
    vector<int> common_element(vector<int>v1,vector<int>v2) {
        sort(v1.begin(), v1.end());
        sort(v2.begin(), v2.end());
        int n = v1.size();
        int m = v2.size();

        int i=0;
        int j=0;

        vector<int> ans;
        while(i<n && j<m) {
        	if(v1[i] == v2[j]) {
        		ans.push_back(v1[i]);
        		v1[i] = INT_MIN;
        		i++;
        		j++;
        	} else if(v1[i] < v2[j]) {
        		i++;
        	} else {
        		j++;
        	}
        }

    	return ans;
    }
0
saitp992 weeks ago
C++
 
vector<int> common_element(vector<int>v1,vector<int>v2)   {       // Your code here       vector<int>res;       map<int,int>mp;       for(auto i:v1)       mp[i]++;       for(auto i:v2)       {           if(mp[i]>=1)           {               res.push_back(i);               mp[i]--;           }       }       sort(res.begin(),res.end());       return res;   }
-1
skzaid96412 weeks ago
//Time taken:3.01s/7.7s
public static ArrayList<Integer> common_element(ArrayList<Integer>v1, ArrayList<Integer>v2)
    {
        Map<Integer,Integer> store = new HashMap<>();
        ArrayList<Integer> res = new ArrayList<Integer>();
        
        for(int i=0;i<v1.size();i++)
            store.put(v1.get(i),store.getOrDefault(v1.get(i),0)+1);
            
        for(int i=0;i<v2.size();i++){
            int ele = v2.get(i);
            if(store.containsKey(ele)){
                int curr = store.get(ele)-1;
                if(curr==0)
                    store.remove(ele);
                else
                    store.put(ele,curr);
                res.add(ele);
            }
        }
        
        Collections.sort(res);
        return res;
    }
0
codernikhil2262 weeks ago
class Solution{
    public:
        vector<int> common_element(vector<int> v1, vector<int> v2)
    {
        // Your code here
        
        map<int, int> m1;
        vector<int> ans;
        
        for (int i = 0; i < v1.size(); i++) m1[v1[i]]++;
        for (int i = 0; i < v2.size(); i++) {
            if (m1.find(v2[i]) != m1.end()) {
                ans.push_back(v2[i]);
                m1[v2[i]]--;
            }
            if (m1[v2[i]] == 0) m1.erase(v2[i]);
        }
        
        sort(ans.begin(), ans.end());
        return ans;
    
    }
};
0
kvatsadeo3 weeks ago
  public:
        vector<int> common_element(vector<int>v1,vector<int>v2)
    {
        // Your code here
        vector<int> v;
        unordered_map<int,int> umap;
        for(int i=0;i<v1.size();i++){
            umap[v1[i]]++;
        }
        for(int i=0;i<v2.size();i++){
            if(umap.find(v2[i])!=umap.end() && umap[v2[i]]>0){
                v.push_back(v2[i]);
                umap[v2[i]]--;
            }
        }
        sort(v.begin(),v.end());
        return v;
    }
0
rajatk3 weeks ago
class Solution{
    public:
        vector<int> common_element(vector<int>v1,vector<int>v2)
    {  map<int,int> mp;vector<int> v;
       for(auto i: v1) mp[i]++;
       for(auto i: v2)
       if(mp[i]) v.push_back(i),mp[i]--;
       sort(v.begin(),v.end());
       return v;
        
        // Your code here
    }
};
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.