Count rows in a matrix that consist of same element
Given a matrix mat[][], the task is to count the number of rows in the matrix that consists of the same elements.
Examples:
Input: mat[][] = {{1, 1, 1}, {1, 2, 3}, {5, 5, 5}}
Output: 2
All the elements of the first row and all the elements of the third row are the same.
Input: mat[][] = {{1, 2}, {4, 2}}
Output: 0
Approach: Set count = 0 and start traversing the matrix row by row and for a particular row, add every element of the row in a set and check if size(set) = 1, if yes then update count = count + 1.
After all the rows have been traversed, print the value of the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countIdenticalRows(vector< vector < int > > mat)
{
int count = 0;
for ( int i = 0; i < mat.size(); i++)
{
set< int > hs;
for ( int j = 0; j < mat[i].size(); j++)
{
hs.insert(mat[i][j]);
}
if (hs.size() == 1)
count++;
}
return count;
}
int main()
{
vector< vector < int > > mat = {{ 1, 1, 1 },
{ 1, 2, 3 },
{ 5, 5, 5 }};
cout << countIdenticalRows(mat);
return 0;
}
|
Java
import java.util.HashSet;
class GFG {
public static int countIdenticalRows( int mat[][])
{
int count = 0 ;
for ( int i = 0 ; i < mat.length; i++) {
HashSet<Integer> hs = new HashSet<>();
for ( int j = 0 ; j < mat[i].length; j++) {
hs.add(mat[i][j]);
}
if (hs.size() == 1 )
count++;
}
return count;
}
public static void main(String[] args)
{
int mat[][] = { { 1 , 1 , 1 },
{ 1 , 2 , 3 },
{ 5 , 5 , 5 } };
System.out.print(countIdenticalRows(mat));
}
}
|
Python3
def countIdenticalRows(mat):
count = 0
for i in range ( len (mat)):
hs = dict ()
for j in range ( len (mat[i])):
hs[(mat[i][j])] = 1
if ( len (hs) = = 1 ):
count + = 1
return count
mat = [ [ 1 , 1 , 1 ],
[ 1 , 2 , 3 ],
[ 5 , 5 , 5 ] ]
print (countIdenticalRows(mat))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int countIdenticalRows( int [,]mat)
{
int count = 0;
for ( int i = 0;
i < mat.GetLength(0); i++)
{
HashSet< int > hs = new HashSet< int >();
for ( int j = 0;
j < mat.GetLength(0); j++)
{
hs.Add(mat[i, j]);
}
if (hs.Count == 1)
count++;
}
return count;
}
public static void Main(String[] args)
{
int [,]mat = {{ 1, 1, 1 },
{ 1, 2, 3 },
{ 5, 5, 5 }};
Console.WriteLine(countIdenticalRows(mat));
}
}
|
Javascript
function countIdenticalRows(mat) {
let count = 0;
for (let i = 0; i < mat.length; i++) {
let set = new Set();
for (let j = 0; j < mat[i].length; j++) {
set.add(mat[i][j]);
}
if (set.size === 1) {
count++;
}
}
return count;
}
const mat = [[1, 1, 1], [1, 2, 3], [5, 5, 5]];
console.log(countIdenticalRows(mat));
|
Complexity Analysis:
- Time Complexity: O(N*M*logM), as we are using nested loops for traversing N*M times. Where N and M are the number of rows and columns respectively.
- Auxiliary Space: O(M), as we are using extra space for the set of size M. Where M is the number of columns in the matrix.
Memory efficient approach: Set count = 0 and start traversing the matrix row by row and, for a particular row, save the first element of the row in a variable first and compare all the other elements with first. If all the other elements of the row are equal to the first element, then update count = count + 1. When all the rows have been traversed, print the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countIdenticalRows( int mat[3][3], int r, int c)
{
int count = 0;
for ( int i = 0; i < r; i++)
{
int first = mat[i][0];
bool allSame = true ;
for ( int j = 1; j < c; j++)
{
if (mat[i][j] != first)
{
allSame = false ;
break ;
}
}
if (allSame)
count++;
}
return count;
}
int main()
{
int mat[][3] = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 5, 5, 2 } };
int row_length = sizeof (mat)/ sizeof (mat[0]) ;
int col_length = sizeof (mat[0])/ sizeof ( int ) ;
cout << countIdenticalRows(mat, row_length,col_length) << endl;
return 0;
}
|
Java
class GFG {
public static int countIdenticalRows( int mat[][])
{
int count = 0 ;
for ( int i = 0 ; i < mat.length; i++) {
int first = mat[i][ 0 ];
boolean allSame = true ;
for ( int j = 1 ; j < mat[i].length; j++) {
if (mat[i][j] != first) {
allSame = false ;
break ;
}
}
if (allSame)
count++;
}
return count;
}
public static void main(String[] args)
{
int mat[][] = { { 1 , 1 , 2 },
{ 2 , 2 , 2 },
{ 5 , 5 , 2 } };
System.out.print(countIdenticalRows(mat));
}
}
|
Python 3
def countIdenticalRows(mat):
count = 0
for i in range ( len (mat)):
first = mat[i][ 0 ]
allSame = True
for j in range ( 1 , len (mat[i])):
if (mat[i][j] ! = first):
allSame = False
break
if (allSame):
count + = 1
return count
if __name__ = = "__main__" :
mat = [[ 1 , 1 , 2 ],
[ 2 , 2 , 2 ],
[ 5 , 5 , 2 ]]
print (countIdenticalRows(mat))
|
C#
using System;
class GFG {
public static int countIdenticalRows( int [,]mat)
{
int count = 0;
for ( int i = 0; i < mat.GetLength(0); i++) {
int first = mat[i,0];
bool allSame = true ;
for ( int j = 1; j < mat.GetLength(1); j++) {
if (mat[i,j] != first) {
allSame = false ;
break ;
}
}
if (allSame)
count++;
}
return count;
}
public static void Main()
{
int [,]mat = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 5, 5, 2 } };
Console.Write(countIdenticalRows(mat));
}
}
|
PHP
<?php
function countIdenticalRows(& $mat )
{
$count = 0;
for ( $i = 0; $i < sizeof( $mat ); $i ++)
{
$first = $mat [ $i ][0];
$allSame = true;
for ( $j = 1; $j < sizeof( $mat [ $i ]); $j ++)
{
if ( $mat [ $i ][ $j ] != $first )
{
$allSame = false;
break ;
}
}
if ( $allSame )
$count ++;
}
return $count ;
}
$mat = array ( array (1, 1, 2),
array (2, 2, 2),
array (5, 5, 2));
echo (countIdenticalRows( $mat ));
?>
|
Javascript
<script>
function countIdenticalRows(mat)
{
let count = 0;
for (let i = 0; i < mat.length; i++) {
let first = mat[i][0];
let allSame = true ;
for (let j = 1; j < mat[i].length; j++) {
if (mat[i][j] != first) {
allSame = false ;
break ;
}
}
if (allSame)
count++;
}
return count;
}
let mat = [[ 1, 1, 2 ],
[2, 2, 2 ],
[5, 5, 2 ]];
document.write(countIdenticalRows(mat));
</script>
|
Complexity Analysis:
- Time Complexity: O(N*M), as we are using nested loops for traversing N*M times. Where N and M are the number of rows and columns respectively.
- Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...