Count rows in a matrix that consist of same element
Last Updated :
24 Mar, 2023
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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count of all identical rows
int countIdenticalRows(vector< vector <int> > mat)
{
int count = 0;
for (int i = 0; i < mat.size(); i++)
{
// HashSet for current row
set<int> hs;
// Traverse the row
for (int j = 0; j < mat[i].size(); j++)
{
// Add all the values of the row in HashSet
hs.insert(mat[i][j]);
}
// Check if size of HashSet = 1
if (hs.size() == 1)
count++;
}
return count;
}
// Driver code
int main()
{
vector< vector <int> > mat = {{ 1, 1, 1 },
{ 1, 2, 3 },
{ 5, 5, 5 }};
cout << countIdenticalRows(mat);
return 0;
}
// This code is contributed by Rituraj Jain
Java
// Java implementation of the approach
import java.util.HashSet;
class GFG {
// Function to return the count of all identical rows
public static int countIdenticalRows(int mat[][])
{
int count = 0;
for (int i = 0; i < mat.length; i++) {
// HashSet for current row
HashSet<Integer> hs = new HashSet<>();
// Traverse the row
for (int j = 0; j < mat[i].length; j++) {
// Add all the values of the row in HashSet
hs.add(mat[i][j]);
}
// Check if size of HashSet = 1
if (hs.size() == 1)
count++;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int mat[][] = { { 1, 1, 1 },
{ 1, 2, 3 },
{ 5, 5, 5 } };
System.out.print(countIdenticalRows(mat));
}
}
Python3
#Function to return the count of all identical rows
def countIdenticalRows(mat):
count = 0
for i in range(len(mat)):
#HashSet for current row
hs=dict()
#Traverse the row
for j in range(len(mat[i])):
#Add all the values of the row in HashSet
hs[(mat[i][j])]=1
#Check if size of HashSet = 1
if (len(hs)== 1):
count+=1
return count
#Driver code
mat= [ [ 1, 1, 1 ],
[ 1, 2, 3 ],
[ 5, 5, 5 ] ]
print(countIdenticalRows(mat))
#This code is contributed by Mohit kumar 29
C#
// C# implementation of
// the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the count
// of all identical rows
public static int countIdenticalRows(int [,]mat)
{
int count = 0;
for (int i = 0;
i < mat.GetLength(0); i++)
{
// HashSet for current row
HashSet<int> hs = new HashSet<int>();
// Traverse the row
for (int j = 0;
j < mat.GetLength(0); j++)
{
// Add all the values
// of the row in HashSet
hs.Add(mat[i, j]);
}
// Check if size of HashSet = 1
if (hs.Count == 1)
count++;
}
return count;
}
// Driver code
public static void Main(String[] args)
{
int [,]mat = {{ 1, 1, 1 },
{ 1, 2, 3 },
{ 5, 5, 5 }};
Console.WriteLine(countIdenticalRows(mat));
}
}
// This code is contributed by Princi Singh
JavaScript
// Function to return the count of all identical rows
function countIdenticalRows(mat) {
let count = 0;
for (let i = 0; i < mat.length; i++) {
// Set for current row
let set = new Set();
// Traverse the row
for (let j = 0; j < mat[i].length; j++) {
// Add all the values of the row in Set
set.add(mat[i][j]);
}
// Check if size of Set = 1
if (set.size === 1) {
count++;
}
}
return count;
}
// Driver code
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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count of all identical rows
int countIdenticalRows(int mat[3][3],int r,int c)
{
int count = 0;
for (int i = 0; i < r; i++)
{
// First element of current row
int first = mat[i][0];
bool allSame = true;
// Compare every element of the current row
// with the first element of the row
for (int j = 1; j < c; j++)
{
// If any element is different
if (mat[i][j] != first)
{
allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if (allSame)
count++;
}
return count;
}
// Driver code
int main()
{
//int mat[3][3] ;
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;
}
// This code is contributed by aishwarya.27
Java
// Java implementation of the approach
class GFG {
// Function to return the count of all identical rows
public static int countIdenticalRows(int mat[][])
{
int count = 0;
for (int i = 0; i < mat.length; i++) {
// First element of current row
int first = mat[i][0];
boolean allSame = true;
// Compare every element of the current row
// with the first element of the row
for (int j = 1; j < mat[i].length; j++) {
// If any element is different
if (mat[i][j] != first) {
allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if (allSame)
count++;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int mat[][] = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 5, 5, 2 } };
System.out.print(countIdenticalRows(mat));
}
}
Python 3
# Python 3 implementation of the approach
# Function to return the count of
# all identical rows
def countIdenticalRows(mat):
count = 0
for i in range(len(mat)):
# First element of current row
first = mat[i][0]
allSame = True
# Compare every element of the current
# row with the first element of the row
for j in range(1, len(mat[i])):
# If any element is different
if (mat[i][j] != first):
allSame = False
break
# If all the elements of the
# current row were same
if (allSame):
count += 1
return count
# Driver code
if __name__ == "__main__":
mat = [[ 1, 1, 2 ],
[2, 2, 2 ],
[5, 5, 2 ]]
print(countIdenticalRows(mat))
# This code is contributed by ita_c
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the count of all identical rows
public static int countIdenticalRows(int [,]mat)
{
int count = 0;
for (int i = 0; i < mat.GetLength(0); i++) {
// First element of current row
int first = mat[i,0];
bool allSame = true;
// Compare every element of the current row
// with the first element of the row
for (int j = 1; j < mat.GetLength(1); j++) {
// If any element is different
if (mat[i,j] != first) {
allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if (allSame)
count++;
}
return count;
}
// Driver code
public static void Main()
{
int [,]mat = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 5, 5, 2 } };
Console.Write(countIdenticalRows(mat));
}
// This code is contributed by Ryuga
}
PHP
<?php
// PHP implementation of the approach
// Function to return the count of
// all identical rows
function countIdenticalRows(&$mat)
{
$count = 0;
for ($i = 0; $i < sizeof($mat); $i++)
{
// First element of current row
$first = $mat[$i][0];
$allSame = true;
// Compare every element of the current
// row with the first element of the row
for ($j = 1; $j < sizeof($mat[$i]); $j++)
{
// If any element is different
if ($mat[$i][$j] != $first)
{
$allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if ($allSame)
$count++;
}
return $count;
}
// Driver code
$mat = array(array(1, 1, 2),
array(2, 2, 2),
array(5, 5, 2));
echo(countIdenticalRows($mat));
// This code is contributed by Shivi_Aggarwal
?>
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the count of all identical rows
function countIdenticalRows(mat)
{
let count = 0;
for (let i = 0; i < mat.length; i++) {
// First element of current row
let first = mat[i][0];
let allSame = true;
// Compare every element of the current row
// with the first element of the row
for (let j = 1; j < mat[i].length; j++) {
// If any element is different
if (mat[i][j] != first) {
allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if (allSame)
count++;
}
return count;
}
// Driver code
let mat = [[ 1, 1, 2 ],
[2, 2, 2 ],
[5, 5, 2 ]];
document.write(countIdenticalRows(mat));
// This code is contributed by rag2127
</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.
Similar Reads
Find distinct elements common to all rows of a matrix Given a n x n matrix. The problem is to find all the distinct elements common to all rows of the matrix. The elements can be printed in any order. Examples: Input : mat[][] = { {2, 1, 4, 3}, {1, 2, 3, 2}, {3, 6, 2, 3}, {5, 2, 5, 3} } Output : 2 3 Input : mat[][] = { {12, 1, 14, 3, 16}, {14, 2, 1, 3,
15+ min read
Row-wise common elements in two diagonals of a square matrix Given a square matrix, find out count of numbers that are same in same row and same in both primary and secondary diagonals. Examples : Input : 1 2 1 4 5 2 0 5 1 Output : 2 Primary diagonal is 1 5 1 Secondary diagonal is 1 5 0 Two elements (1 and 5) match in two diagonals and same. Input : 1 0 0 0 1
4 min read
Count of odd sum Submatrix with odd element count in the Matrix Given a matrix mat[][] of size N x N, the task is to count the number of submatrices with the following properties: The sum of all elements in the submatrix is odd.The number of elements in the submatrix is odd.Examples: Input: mat[][] = {{1, 2, 3}, {7, 5, 9}, {6, 8, 10}}Output: 8Explanation: As her
15 min read
Count of unique rows in a given Matrix Given a 2D matrix arr of size N*M containing lowercase English letters, the task is to find the number of unique rows in the given matrix. Examples: Input: arr[][]= { {'a', 'b', 'c', 'd'}, {'a', 'e', 'f', 'r'}, {'a', 'b', 'c', 'd'}, {'z', 'c', 'e', 'f'} }Output: 2Explanation: The 2nd and the 4th row
10 min read
Common elements in all rows of a given matrix Given an m x n matrix, find all common elements present in all rows in O(mn) time and one traversal of matrix.Example: Input:mat[4][5] = {{1, 2, 1, 4, 8}, {3, 7, 8, 5, 1}, {8, 7, 7, 3, 1}, {8, 1, 2, 7, 9}, };Output: 1 8 or 8 18 and 1 are present in all rows.A simple solution is to consider every ele
7 min read