# Number of rows and columns in a Matrix that contain repeated values

Given a

N x N

square matrix

arr[][]

which contains only integers between 1 and N, the task is to compute the number of rows and the number of columns in the matrix that contain repeated values.

Examples:

Input: N = 4, arr[][] = {{1, 2, 3, 4}, {2, 1, 4, 3}, {3, 4, 1, 2}, {4, 3, 2, 1}} Output: 0 0 Explanation: None of the rows or columns contain repeated values. Input: N = 4, arr[][]= {{2, 2, 2, 2}, {2, 3, 2, 3}, {2, 2, 2, 3}, {2, 2, 2, 2}} Output: 4 4 Explanation: In every column and every row of the square matrix, the values are repeated. Therefore, the total count is 4 for both rows and columns.

Approach:

The idea is to use the

NumPy library

.

• Make a NumPy array of every row and every column in the square matrix.
• Find the length of the unique elements.
• If the length is equal to N then, there are no repeated values present in that particular row or column.

Below is the implementation of the above approach:

## C++

 `// Nikunj Sonigara`   `#include ` `#include ` `#include ` `using` `namespace` `std;`   `// Function to count the number of rows and columns` `// that contain repeated values in a square matrix.` `pair<``int``, ``int``> repeated_val(``int` `N, vector>& matrix) {` `    ``int` `column = 0;` `    ``int` `row = 0;`   `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// For every row, create an unordered_set to track unique elements.` `        ``unordered_set<``int``> rowSet(matrix[i].begin(), matrix[i].end());`   `        ``// The size of the unordered_set should be equal to N for all unique elements.` `        ``if` `(rowSet.size() != N) {` `            ``row++;` `        ``}`   `        ``// For every column, create an unordered_set to track unique elements.` `        ``unordered_set<``int``> colSet;`   `        ``for` `(``int` `j = 0; j < N; j++) {` `            ``colSet.insert(matrix[j][i]);` `        ``}`   `        ``// The size of the unordered_set should be equal to N for all unique elements.` `        ``if` `(colSet.size() != N) {` `            ``column++;` `        ``}` `    ``}`   `    ``// Returning the count of rows and columns` `    ``return` `make_pair(row, column);` `}`   `int` `main() {` `    ``int` `N = 3;` `    ``vector> matrix = {{2, 1, 3}, {1, 3, 2}, {1, 2, 3}};`   `    ``pair<``int``, ``int``> result = repeated_val(N, matrix);`   `    ``cout << result.first << ``" "` `<< result.second << endl;` `    ``return` `0;` `}`

## Java

 `// Nikunj Sonigara`   `import` `java.util.HashSet;` `import` `java.util.Set;`   `public` `class` `Main {` `    ``public` `static` `void` `main(String[] args) {` `        ``int` `N = ``3``;` `        ``int``[][] matrix = {{``2``, ``1``, ``3``}, {``1``, ``3``, ``2``}, {``1``, ``2``, ``3``}};`   `        ``int``[] result = repeatedVal(N, matrix);`   `        ``System.out.println(result[``0``] + ``" "` `+ result[``1``]);` `    ``}`   `    ``public` `static` `int``[] repeatedVal(``int` `N, ``int``[][] matrix) {` `        ``int` `column = ``0``;` `        ``int` `row = ``0``;`   `        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// For every row, create a set to track unique elements.` `            ``Set rowSet = ``new` `HashSet<>();` `            ``for` `(``int` `j = ``0``; j < N; j++) {` `                ``rowSet.add(matrix[i][j]);` `            ``}`   `            ``// The size of the set should be equal to N for all unique elements.` `            ``if` `(rowSet.size() != N) {` `                ``row++;` `            ``}`   `            ``// For every column, create a set to track unique elements.` `            ``Set colSet = ``new` `HashSet<>();` `            ``for` `(``int` `j = ``0``; j < N; j++) {` `                ``colSet.add(matrix[j][i]);` `            ``}`   `            ``// The size of the set should be equal to N for all unique elements.` `            ``if` `(colSet.size() != N) {` `                ``column++;` `            ``}` `        ``}`   `        ``return` `new` `int``[] {row, column};` `    ``}` `}`

## Python3

 `# Python program to count the number of ` `# rows and columns in a square matrix ` `# that contain repeated values`   `import` `numpy as np`   `# Function to count the number of rows` `# and number of columns that contain ` `# repeated values in a square matrix.` `def` `repeated_val(N, matrix):` `    ``column ``=` `0` `    ``row ``=` `0` `    ``for` `i ``in` `range` `(N):`   `    ``# For every row, an array is formed. ` `    ``# The length of the unique elements ` `    ``# is calculated, which if not equal ` `    ``# to 'N' then the row has repeated values.` `        ``if` `(``len``(np.unique(np.array(matrix[i])))!``=` `N):` `            ``row ``+``=` `1`   `    ``# For every column, an array is formed.` `    ``# The length of the unique elements ` `    ``# is calculated, which if not equal ` `    ``# to N then the column has repeated values.` `    ``for` `j ``in` `range` `(N):` `        ``if` `(``len``(np.unique(np.array([m[j] ``for` `m ``in` `matrix])))!``=` `N):` `            ``column ``+``=` `1` `            `  `    ``# Returning the count of ` `    ``# rows and columns` `    ``return` `row, column`     `# Driver code` `if` `__name__ ``=``=` `'__main__'``: `   `   `  `    ``N ``=` `3` `    ``matrix ``=` `[ [ ``2``, ``1``, ``3` `], [ ``1``, ``3``, ``2` `], [ ``1``, ``2``, ``3` `] ]`   `    ``print``(repeated_val(N, matrix))  `

## Javascript

 `// Nikunj Sonigara`   `function` `repeatedVal(N, matrix) {` `    ``let column = 0;` `    ``let row = 0;`   `    ``for` `(let i = 0; i < N; i++) {` `        ``// For every row, create a Set to track unique elements.` `        ``let rowSet = ``new` `Set(matrix[i]);`   `        ``// The size of the Set should be equal to N for all unique elements.` `        ``if` `(rowSet.size !== N) {` `            ``row++;` `        ``}`   `        ``// For every column, create a Set to track unique elements.` `        ``let colSet = ``new` `Set();` `        ``for` `(let j = 0; j < N; j++) {` `            ``colSet.add(matrix[j][i]);` `        ``}`   `        ``// The size of the Set should be equal to N for all unique elements.` `        ``if` `(colSet.size !== N) {` `            ``column++;` `        ``}` `    ``}`   `    ``return` `[row, column];` `}`   `function` `main() {` `    ``const N = 3;` `    ``const matrix = [[2, 1, 3], [1, 3, 2], [1, 2, 3]];`   `    ``const result = repeatedVal(N, matrix);`   `    ``console.log(result[0] + ``" "` `+ result[1]);` `}`   `main();`

Output

```(0, 2)

```

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next