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:

`# 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)) ` |

*chevron_right*

*filter_none*

**Output:**

(0, 2)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Remove first X rows and columns from a matrix
- Remove any corner X rows and columns from a matrix
- Enlarge a Matrix such that each element occurs in R rows and C columns
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Maximum increase in value of Matrix to keep maximum rows and columns unchanged
- Count rows/columns with sum equals to diagonal sum
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Sum of columns of a 2-D Matrix where first element is odd
- Interchange elements of first and last columns in matrix
- Count all the columns in a matrix which are sorted in descending
- Count all sorted rows in a matrix
- Interchange elements of first and last rows in matrix
- Find all permuted rows of a given row in a matrix
- Common elements in all rows of a given matrix
- Print unique rows in a given boolean matrix
- Check if all rows of a matrix are circular rotations of each other
- Count rows in a matrix that consist of same element
- Maximum difference of sum of elements in two rows in a matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.