Find distinct elements | Practice | GeeksforGeeks

Given a N x N matrix M. Write a program to find count of all the distinct elements common to all rows of the matrix. Print count of such elements.
Example 1:
Input: 
N = 4
M = {{2, 1, 4, 3},
     {1, 2, 3, 2},
     {3, 6, 2, 3},
     {5, 2, 5, 3}}
Output: 
2
Explaination: Only 2 and 3 are common in all rows.
Example 2:
Input: 
N = 5
M = {{12, 1, 14, 3, 16},
     {14, 2, 1, 3, 35},
     {14, 1, 14, 3, 11},
     {14, 5, 3, 2, 1},
     {1, 18, 3, 21, 14}}
Output: 
3
Explaination: 14, 3 and 1 are common in all the rows.
Your Task:
You do not need to read input or print anything. Your task is to complete the function distinct() which takes the matrix and n as input parameters and returns the number of distinct elements present in every row.
Expected Time Complexity: O(N*N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 100
1 ≤ M[i][j] ≤ 1000
0
naveenapatnaik2 weeks ago
JAVA Solution!!
   static int distinct(int M[][], int N)   {       // code here       Collection<Integer> removeElements = new LinkedList<Integer>();       HashSet<Integer> SETValue=new HashSet<>();       for (int i=0; i<N; i++)           SETValue.add(M[0][i]);       for (int i=1; i<N; i++)       {           HashSet<Integer> temp= new HashSet<Integer>();           for (int j=0; j<N; j++)               temp.add(M[i][j]);           for(Integer element : SETValue)               if(!(temp.contains(element)))                   removeElements.add(element);           SETValue.removeAll(removeElements);           if (SETValue.size() == 0)               break;       }       return SETValue.size();          }
0
ektabangar3 weeks ago
Java Code
class Solution{   static int distinct(int M[][], int N)   {       // code here       HashSet<Integer> hs=new HashSet<>();       for(int i=0;i<M[0].length;i++){           hs.add(M[0][i]);       }       for(int i=1;i<M.length;i++){           HashSet<Integer> temp = new HashSet<>();           for(int j=0;j<M[i].length;j++){              temp.add(M[i][j]);           }           hs.retainAll(temp);       }       return hs.size();   }}
0
harendraseervi1234567893 weeks ago
C++ Solution
int distinct(vector<vector<int>> M, int n)
   {
       int maxi=INT_MIN;
       vector<int>arr[1000+1];
       for(int i=0;i<n;i++){
           unordered_map<int,int>mp;
           for(int j=0;j<n;j++){
               mp[M[i][j]]++;
               maxi=max(maxi,M[i][j]);
           }
           for(auto val:mp){
               arr[val.first].push_back(i);
           }
       }
       
       int ct=0;
       for(int i=0;i<=maxi;i++){
           if(arr[i].size()==n) ct++;
       }
       return ct;
   
   }
0
harshpandeyalfa23 weeks ago
C++
(0.51sec)
int distinct(vector<vector<int>> M, int N)   {              unordered_map<int,int> s1,s2,s3;                for(int i=0;i<N;i++)         s1[M[0][i]]++;                 for(int i=1;i<N;i++)        {            for(int j=0;j<N;j++)            {                s2[M[i][j]]++;            }            for(auto it=s1.begin();it!=s1.end();it++)            {                                if(s2.find(it->first)!=s2.end())                 s3[it->first]++;            }            s2.clear();        }        int count=0;         for(auto it=s3.begin();it!=s3.end();it++)        {            if(it->second ==N-1)             count++;        }        return count;   }
+1
aloksinghbais024 weeks ago
C++ solution having time complexity as O(n*n+1000) and space complexity as O(1000) is as follows :- 
 
Execution Time :- 0.2 / 1.37 sec
 
int distinct(vector<vector<int>> M, int N){       int freq[1001] = {0};       for(int i = 0; i < N; i++){           bool vis[1001] = {false};           for(int j = 0; j < N; j++){               if(!vis[M[i][j]]){                   vis[M[i][j]] = true;                   freq[M[i][j]]++;               }           }       }              int cnt = 0;       for(int i = 0; i <= 1000; i++){           if(freq[i] == N) cnt++;       }              return (cnt);   }
0
dell94591 month ago
Java:class Solution{   static int distinct(int M[][], int N)   {       // code here       int count =0;       Set<Integer> newSet = new HashSet<>();       for (int i =0; i<N;i++){           newSet.add(M[0][i]);       }       for (int i = 1; i < M.length; i++) {       Set<Integer> compareSet = new HashSet<>();       for (int j = 0; j < M[0].length; j++) {           compareSet.add(M[i][j]);       }           newSet.retainAll(compareSet);       }       count = newSet.size();       return count;   }}
0
9chara91 month ago
Only one time traversal through the MTime Complexity: O(N*N)
    int distinct(vector<vector<int>> M, int N)
   {
       // code here
       int res = 0;
       unordered_map<int, int> counts;
       unordered_set<int> indexes[N];
       
       for (int i = 0; i < N; i++)
       {
           for (int j = 0; j < N; j++)
           {
               if (indexes[i]
               .insert(M[i][j]).second)
               {
                   counts[M[i][j]]++;
                   if (counts[M[i][j]] == N)
                       res++;
               }
           }
       }
       
       return res;
   }
0
abytespaceneeded1 month ago
class Solution{
public:
    int distinct(vector<vector<int>> M, int N)
    {
        // code here
        unordered_map<int,int> m;
        
        for(int i=0;i<N;i++)
        {
            set<int> s;
            for(int j=0;j<N;j++)
            {
                s.insert(M[i][j]);
            }
            
            for(auto it:s)
                m[it]++;
            
            s.clear();
        }
        int count = 0;
        for(auto it: m)
        {
            if(it.second == N)
                ++count;
        }
        
        return count;
    }
};
0
abinashm200674ca2 months ago
int distinct(vector<vector<int>> M, int n)   {       map<int,int> mp;       for(int i=0;i<n;i++)       {           for(int j=0;j<n;j++)           {               if(mp[M[i][j]]==i)               {                   mp[M[i][j]]++;               }           }       }        int i=0;       for(auto x:mp)       {           if(x.second==n)i++;       }       return i;   }
0
gunjapandey3 months ago
class Solution{
    static int distinct(int M[][], int N)
    {
        // code here
        Set<Integer> a = new HashSet<Integer>();
        for(int i=0;i<N;i++)
        {
            a.add(M[0][i]);
        }
        for(int i=1;i<N;i++)
        {
            Set<Integer> n = new HashSet<Integer>();
            for(int j=0;j<N;j++)
            {
                n.add(M[i][j]);
            }
            a.retainAll(n);
        }
        return a.size();
    }
}
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.