Question:
How to find duplicate rows in a binary matrix using C++

Summary

Consider the following aspect:

Give boolean matrix size where each of the cells includes either 0 or 1, and find out the row numbers (0-based) of rows that are repeated or are already exist.  


Let’s assume:

Function- repeatedRows

Counting Occurances- map<vector<int>, int> mp;


Solution


//User function Template for C++


class Solution

{   

    public:

    vector<int> repeatedRows(vector<vector<int>> &matrix, int M, int N) 

    { 

        // Your code here

        map<vector<int>,int> mp;

        for(int i=0;i<M;i++) mp[matrix[i]]++;

        vector<int> ans;

        map<vector<int>,int> mp2;

        for(auto it : mp) {

            if(it.second>=2) mp2[it.first] = it.second;

        }

        

        for(auto it : mp2){

            deque<int> dq;

            for(int i=0;i<M;i++) {

                if(it.first==matrix[i]) dq.push_back(i);

            }

            dq.pop_front();

            ans.insert(ans.end(), dq.begin(), dq.end());

            

        }

        sort(ans.begin(),ans.end());

        return ans;

    } 

};


Suggested blogs:

>Find the number of pairs of elements whose sum is equal to K using C++

>How to check whether string is palindrome or not using C++

>How to determine the smallest possible size of a vertex cover using C++

>Build an Electron application from scratch

>Building Web API using ASP.NET (C#)

>Built Web API using ASP.NET (C#)


Nisha Patel

Nisha Patel

Submit
0 Answers