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 check if a matrix columns contain numbers without repetition in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <unordered_set>

bool areColumnsUnique(const std::vector<std::vector<int>>& matrix) {
    if (matrix.empty() || matrix[0].empty()) return true; // Handle empty matrix

    int rows = matrix.size();
    int cols = matrix[0].size();

    for (int col = 0; col < cols; ++col) {
        std::unordered_set<int> seen;
        for (int row = 0; row < rows; ++row) {
            if (seen.find(matrix[row][col]) != seen.end()) {
                return false; // Duplicate found in the column
            }
            seen.insert(matrix[row][col]);
        }
    }
    return true; // All columns have unique numbers
}

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

    if (areColumnsUnique(matrix)) {
        std::cout << "All columns contain unique numbers.\n";
    } else {
        std::cout << "Some columns contain duplicate numbers.\n";
    }
}

  
  
/*
run:
  
All columns contain unique numbers.
  
*/

 



answered May 28, 2025 by avibootz
...