Number of rows and columns in a Matrix that contain repeated values
Last Updated :
10 Jan, 2024
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 <iostream>
#include <vector>
#include <unordered_set>
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<vector<int>>& 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<vector<int>> 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<Integer> 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<Integer> 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};
}
}
Python
def repeated_val(N, matrix):
# Initialize counters for rows and columns with repeated values
column = 0
row = 0
# Iterate through each row in the matrix
for i in range(N):
# For every row, create a set to track unique elements.
row_set = set(matrix[i])
# The length of the set should be equal to N for all unique elements.
# If not, increment the row counter.
if len(row_set) != N:
row += 1
# For every column, create a set to track unique elements.
col_set = set()
# Iterate through each element in the current column
for j in range(N):
col_set.add(matrix[j][i])
# The length of the set should be equal to N for all unique elements.
# If not, increment the column counter.
if len(col_set) != N:
column += 1
# Returning the count of rows and columns with repeated values
return row, column
if __name__ == '__main__':
# Sample input
N = 3
matrix = [[2, 1, 3], [1, 3, 2], [1, 2, 3]]
# Call the function and get the result
result = repeated_val(N, matrix)
# Print the result
print(result[0], result[1])
C#
using System;
using System.Collections.Generic;
class Program {
// Function to count the number of rows and columns
// that contain repeated values in a square matrix.
static Tuple<int, int>
RepeatedValues(int N, List<List<int> > matrix)
{
int column = 0;
int row = 0;
for (int i = 0; i < N; i++) {
// For every row, create a HashSet to track
// unique elements.
HashSet<int> rowSet
= new HashSet<int>(matrix[i]);
// The size of the HashSet should be equal to N
// for all unique elements.
if (rowSet.Count != N) {
row++;
}
// For every column, create a HashSet to track
// unique elements.
HashSet<int> colSet = new HashSet<int>();
for (int j = 0; j < N; j++) {
colSet.Add(matrix[j][i]);
}
// The size of the HashSet should be equal to N
// for all unique elements.
if (colSet.Count != N) {
column++;
}
}
// Returning the count of rows and columns
return new Tuple<int, int>(row, column);
}
static void Main()
{
int N = 3;
List<List<int> > matrix = new List<List<int> >{
new List<int>{ 2, 1, 3 },
new List<int>{ 1, 3, 2 },
new List<int>{ 1, 2, 3 }
};
Tuple<int, int> result = RepeatedValues(N, matrix);
Console.WriteLine(result.Item1 + " "
+ result.Item2);
}
}
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();
Time Complexity: O(N2)
Auxiliary Space: O(N), where N is the size of the square matrix
Similar Reads
Count zeros in a row wise and column wise sorted matrix Given a n x n binary matrix (elements in matrix can be either 1 or 0) where each row and column of the matrix is sorted in ascending order, count number of 0s present in it.Examples: Input: [0, 0, 0, 0, 1][0, 0, 0, 1, 1][0, 1, 1, 1, 1][1, 1, 1, 1, 1][1, 1, 1, 1, 1]Output: 8Input: [0, 0][0, 0]Output:
6 min read
Count of numbers in given range L to R which are present in a Matrix Given a matrix(mat[][]), which is sorted row and column-wise in increasing order. Two integers are given, L and R, our task is to count number of elements of the matrix within the range [L, R].Examples: Input: L = 3, R = 11, matrix = {{1, 6, 9} {2, 7, 11} {3, 8, 12}} Output: 6 Explanation: The eleme
8 min read
Count number of free cell present in the Matrix Given a Matrix size N*N and an integer K. Initially, the matrix contains only 0. You are given K tasks and for each task, you are given two coordinates (r, c). Where coordinates (r, c) denotes the rth row and the cth column of the given matrix. You have to perform each task sequentially in the given
11 min read
Count number of 1's at specific position in a matrix - II Given a binary matrix mat[][] of size m*n (0-based indexing), the task is to return the total number of 1's in the matrix that follows the below two rules. i.e: Row R and column C both contain exactly N number of 1's.All rows with 1's in column C should be the same as the 1's present in row R.Exampl
13 min read
Number of connected components in a 2-D matrix of strings Given a 2-D matrix mat[][] the task is to count the number of connected components in the matrix. A connected component is formed by all equal elements that share some common side with at least one other element of the same component.Examples: Input: mat[][] = {"bbba", "baaa"} Output: 2 The two conn
8 min read