Find duplicate rows in a binary matrix | Practice | GeeksforGeeks

Given a boolean matrix of size RxC where each cell contains either 0 or 1, find the row numbers of row (0-based) which already exists or are repeated.
Example 1:
Input:
R = 2, C = 2
matrix[][] = {{1, 0},
              {1, 0}}
Output: 
1
Explanation:
Row 1 is duplicate of Row 0.

Example 2:
Input:
R = 4, C = 3
matrix[][] = {{ 1, 0, 0},
              { 1, 0, 0},
              { 1, 0, 0},
              { 0, 0, 0}}
Output: 
1 2 
Explanation:
Row 1 and Row 2 are duplicates of Row 0. 

Your Task:
You dont need to read input or print anything. Complete the function repeatedRows() that takes the matrix as input parameter and returns a list of row numbers which are duplicate rows.
 
Expected Time Complexity: O(R * C)
Expected Auxiliary Space: O(R*C) 
 
Constraints:
1 ≤ R ≤ 1000
1 ≤ C ≤ 20
0 ≤ matrix[i][j] ≤ 1
0
prajjwalfire5 days ago
BIT Masking for each row and Map
vector<int> repeatedRows(vector<vector<int>> matrix, int M, int N) 
{ 
    unordered_map<int,int> ma;
    vector<int> kim;
    for(int r=0;r<M;r++)
    {
        int num=0;
        
        for(int c=0;c<N;c++)
        {
            if(matrix[r][c])
            {
                num=num|(1<<c);
            }
        }
        
        if(ma.count(num))
            kim.push_back(r);
        else
            ma[num]++;
    }
    
    return kim;
} 
0
shuklaps11192 weeks ago
//1.3/6.28 java solution.......

class Solution
{
    public static ArrayList<Integer> repeatedRows(int matrix[][], int m, int n)
    {
        //code here
        HashSet<String> s = new HashSet<>();
        ArrayList<Integer> al = new ArrayList<>();
        for(int i=0;i<m;i++){
            String str = "";
            for(int j=0;j<n;j++)
                str += matrix[i][j];
            
            
            if(s.contains(str))
                al.add(i);
            else
                s.add(str);
            
        }
        
        return al;
    }
}
0
shilpfizzy4 weeks ago
class Solution{   public:vector<int> repeatedRows(vector<vector<int>> matrix, int M, int N) {    set<int> st;   int deci;   vector<int> res;   for(int i=0; i<M; i++){       deci = 0;       for(int j=0; j<N; j++){           deci += (matrix[i][j] * pow(2,j));       }           if(st.find(deci) != st.end()){               res.push_back(i);           }           else{               st.insert(deci);           }          } return res;} };
0
shibombhowmik12091 month ago
struct Node{
    Node *links[2];
    bool flag=false;
    
    bool containsKey(int x){
        return (links[x]!=NULL);
    }
    
    void put(int x,Node *node){
        links[x]=node;
    }
    
    Node *get(int x){
        return links[x];
    }
    
    void setEnd(){
        flag=true;
    }
    
    bool getEnd(){
        return flag;
    }
};

class Trie{
    private:Node *root;
    public:
            Trie(){
                root=new Node();
            }
            
            void insert(vector<int> v){
                Node *node=root;
                for(int i=0;i<v.size();i++){
                    if(!node->containsKey(v[i])){
                        node->put(v[i],new Node());
                    }
                    
                    node=node->get(v[i]);
                }
                
                node->setEnd();
            }
            
            bool ispresent(vector<int> v){
                Node *node=root;
                for(int i=0;i<v.size();i++){
                    if(node->containsKey(v[i])){
                       node=node->get(v[i]); 
                    }
                    else{
                        return false;
                    }
                }
                
                return node->getEnd();
            }
};

class Solution
{   
public:
vector<int> repeatedRows(vector<vector<int>> matrix, int M, int N) 
{ 
    // Your code here
    Trie *obj=new Trie();
    vector<int> res;
    
    for(int i=0;i<M;i++){
        vector<int> v;
        for(int j=0;j<N;j++){
            v.emplace_back(matrix[i][j]);
        }
        
        if(obj->ispresent(v)){
            res.emplace_back(i);
        }
        obj->insert(v);
    }
    
    return res;
} 
};
+1
vt28522412 months ago
Easy Java solution using HashSet
public static ArrayList<Integer> repeatedRows(int matrix[][], int m, int n)   {       HashSet<String> set=new HashSet<>();       ArrayList<Integer> list=new ArrayList<>();       for(int i=0; i<m; i++){           String str="";           for(int j=0; j<n; j++){               str+=matrix[i][j];           }           if(set.contains(str)){               list.add(i);           }           else{               set.add(str);           }       }              return list;   }
0
kumaarsahab4322 months ago
//Using HashMap
vector<int> repeatedRows(vector<vector<int>> matrix, int M, int N) 
    { 
        map<vector<int>, int> m ;
        vector<int> ans; 
        for(int i=0 ; i<M ; i++)
        {
            vector<int> x=matrix[i] ;
            if(m[x]>0) ans.push_back(i) ;
            m[x]++ ;
        }
        return ans ; 
    }
+1
himanshujain4572 months ago
Simple  Java Solution Using Trie:
class Solution{   static class TrieNode{       int wordend=0;       TrieNode child[]=new TrieNode[2];       TrieNode()       {           for(int i=0;i<2;i++)           {               child[i]=null;           }       }   }   static TrieNode root=new TrieNode();   static void BuildTrie(int matrix[][], int m, int n)   {       for(int i=0;i<m;i++)       {       TrieNode temp=root;           for(int j=0;j<n;j++)           {               if(temp.child[matrix[i][j]]==null)               {                   temp.child[matrix[i][j]]=new TrieNode();               }               temp=temp.child[matrix[i][j]];           }           temp.wordend=1;       }          }   public static ArrayList<Integer> repeatedRows(int matrix[][], int m, int n)   {       ArrayList<Integer>res=new ArrayList<Integer>();       BuildTrie(matrix,m,n);        for(int i=0;i<m;i++)       {       TrieNode temp=root;                  for(int j=0;j<n;j++)           {                              temp=temp.child[matrix[i][j]];           }           if(temp.wordend==1)           {           temp.wordend=0;           }           else           {               res.add(i);           }       }       return res;                 }}
+1
abisheksrinivasan3 months ago
Simple Python soln:
def repeatedRows(self, a, m ,n):
        strings, res = set(), []
        for r in range(m):
            s=''
            for c in range(n):
                s+=str(a[r][c])
            if s in strings:
                res.append(r)
            else:
                strings.add(s)
        return res
0
paurushbatish3 months ago
Easy question  →
vector<int> repeatedRows(vector<vector<int>> matrix, int M, int N) 
{ 
    // Your code here
    map<vector<int>, vector<int>>m;
    vector<int>ans;
    for(int i = 0;i<M;i++){
        vector<int>x = matrix[i];
        if(m[x].size()!=0){
            ans.push_back(i);
        }
        m[x].push_back(i);
        
    }
    
 return ans;
}
0
abhishekpundir5263 months ago
    string s = "";   set<string>st;   vector<int>ans;      for(int i = 0;i < matrix.size();i++){       s = "";       for(int j = 0;j < matrix[i].size();j++){           s = s + to_string(matrix[i][j]);       }       if(st.find(s) != st.end()){           ans.push_back(i);       }       else{           st.insert(s);       }   }      return ans; 
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.