Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,970 questions

51,912 answers

573 users

How to find repeated rows of a matrix in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <unordered_map>

// Helper function to convert a vector to a string (for row comparison)
std::string vectorToString(const std::vector<int>& row) {
    std::ostringstream oss;
    for (int num : row) {
        oss << num << ",";
    }
    return oss.str();
}

void findRepeatedRows(const std::vector<std::vector<int>>& matrix) {
    std::unordered_map<std::string, int> rowCount;

    for (const auto& row : matrix) {
        std::string pattern = vectorToString(row);
        rowCount[pattern]++;
    }

    std::cout << "Repeated Rows:\n";
    for (const auto& entry : rowCount) {
        if (entry.second > 1) {
            std::cout << "Row: [" << entry.first << "] - Repeated " << entry.second << " times\n";
        }
    }
}

int main() {
    std::vector<std::vector<int>> matrix = {
        {1, 2, 3},
        {4, 5, 6},
        {1, 2, 3},
        {7, 8, 9},
        {4, 5, 6},
        {0, 1, 2}, 
        {4, 5, 6},
    };

    findRepeatedRows(matrix);
}

 
 
/*
run:
 
Repeated Rows:
Row: [4,5,6,] - Repeated 3 times
Row: [1,2,3,] - Repeated 2 times
 
*/

 



answered May 23, 2025 by avibootz
...