Program to check the common elements in the Row’s of Matrix
Definition & Explanation
Example:
Input:
Output:
More Description
Program Logic
Desc
C
Method 1 :
#include <stdio.h>
void findCommonElements(int matrix[][3], int rows, int cols) {
if (rows == 0 || cols == 0)
return;
// Convert the first row to a set
int commonElements[cols];
for (int j = 0; j < cols; j++)
commonElements[j] = matrix[0][j];
// Find the intersection of sets for each subsequent row
for (int i = 1; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (commonElements[j] != matrix[i][j])
commonElements[j] = -1;
}
}
// Print the common elements
printf("Common elements in all rows: ");
for (int j = 0; j < cols; j++) {
if (commonElements[j] != -1)
printf("%d ", commonElements[j]);
}
printf("\n");
}
int main() {
int matrix[3][3] = {
{1, 2, 3},
{2, 3, 4},
{3, 4, 5}
};
int rows = 3, cols = 3;
findCommonElements(matrix, rows, cols);
return 0;
}
Output :
C++
Method 1 :
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
void findCommonElements(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[0].empty())
return;
unordered_set<int> commonElements(matrix[0].begin(), matrix[0].end());
// Find the intersection of sets for each subsequent row
for (int i = 1; i < matrix.size(); i++) {
unordered_set<int> rowSet(matrix[i].begin(), matrix[i].end());
for (auto it = commonElements.begin(); it != commonElements.end();) {
if (rowSet.find(*it) == rowSet.end())
it = commonElements.erase(it);
else
++it;
}
}
// Print the common elements
cout << "Common elements in all rows: ";
for (int num : commonElements)
cout << num << " ";
cout << endl;
}
int main() {
vector<vector<int>> matrix = {
{1, 2, 3},
{2, 3, 4},
{3, 4, 5}
};
findCommonElements(matrix);
return 0;
}
Output :
JAVA
Method 1 :
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void findCommonElements(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0)
return;
Set<Integer> commonElements = new HashSet<>();
for (int num : matrix[0])
commonElements.add(num);
// Find the intersection of sets for each subsequent row
for (int i = 1; i < matrix.length; i++) {
Set<Integer> rowSet = new HashSet<>();
for (int num : matrix[i])
rowSet.add(num);
commonElements.retainAll(rowSet);
}
// Print the common elements
System.out.print("Common elements in all rows: ");
for (int num : commonElements)
System.out.print(num + " ");
System.out.println();
}
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{2, 3, 4},
{3, 4, 5}
};
findCommonElements(matrix);
}
}
Output :
Python
Method 1 :
def common_elements(matrix):
if not matrix:
return []
# Convert the first row to a set
common_elements_set = set(matrix[0])
# Find the intersection of sets for each subsequent row
for row in matrix[1:]:
common_elements_set.intersection_update(row)
return list(common_elements_set)
# Example usage:
matrix = [
[1, 2, 3],
[2, 3, 4],
[3, 4, 5]
]
result = common_elements(matrix)
print("Common elements in all rows:", result) # Output: [3]
Output :
Common elements in all rows: [3]