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++
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
pair< int , int > repeated_val( int N, vector<vector< int >>& matrix) {
int column = 0;
int row = 0;
for ( int i = 0; i < N; i++) {
unordered_set< int > rowSet(matrix[i].begin(), matrix[i].end());
if (rowSet.size() != N) {
row++;
}
unordered_set< int > colSet;
for ( int j = 0; j < N; j++) {
colSet.insert(matrix[j][i]);
}
if (colSet.size() != N) {
column++;
}
}
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
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++) {
Set<Integer> rowSet = new HashSet<>();
for ( int j = 0 ; j < N; j++) {
rowSet.add(matrix[i][j]);
}
if (rowSet.size() != N) {
row++;
}
Set<Integer> colSet = new HashSet<>();
for ( int j = 0 ; j < N; j++) {
colSet.add(matrix[j][i]);
}
if (colSet.size() != N) {
column++;
}
}
return new int [] {row, column};
}
}
|
Python
def repeated_val(N, matrix):
column = 0
row = 0
for i in range (N):
row_set = set (matrix[i])
if len (row_set) ! = N:
row + = 1
col_set = set ()
for j in range (N):
col_set.add(matrix[j][i])
if len (col_set) ! = N:
column + = 1
return row, column
if __name__ = = '__main__' :
N = 3
matrix = [[ 2 , 1 , 3 ], [ 1 , 3 , 2 ], [ 1 , 2 , 3 ]]
result = repeated_val(N, matrix)
print (result[ 0 ], result[ 1 ])
|
C#
using System;
using System.Collections.Generic;
class Program {
static Tuple< int , int >
RepeatedValues( int N, List<List< int > > matrix)
{
int column = 0;
int row = 0;
for ( int i = 0; i < N; i++) {
HashSet< int > rowSet
= new HashSet< int >(matrix[i]);
if (rowSet.Count != N) {
row++;
}
HashSet< int > colSet = new HashSet< int >();
for ( int j = 0; j < N; j++) {
colSet.Add(matrix[j][i]);
}
if (colSet.Count != N) {
column++;
}
}
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
function repeatedVal(N, matrix) {
let column = 0;
let row = 0;
for (let i = 0; i < N; i++) {
let rowSet = new Set(matrix[i]);
if (rowSet.size !== N) {
row++;
}
let colSet = new Set();
for (let j = 0; j < N; j++) {
colSet.add(matrix[j][i]);
}
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
Share your thoughts in the comments
Please Login to comment...