Find unique elements in a matrix
Last Updated :
19 Dec, 2024
Given a matrix mat[][] having n rows and m columns. The task is to find unique elements in the matrix i.e., those elements which are not repeated in the matrix or those elements whose frequency is 1.
Examples:
Input:
mat[][] = [[2, 1, 4, 3],
[1, 2, 3, 2],
[3, 6, 2, 3],
[5, 2, 5, 3]]
Output: 4 6
Input:
mat[][] = [[1, 2],
[2, 1]]
Output: No unique element in the matrix
Approach:
The idea is to create an empty hashmap and traverse through all the elements of the matrix and update the count of element in hashmap. After traversal if count of element is 1 it is unique else it is not.
C++
// C++ program to find unique element in matrix
#include <bits/stdc++.h>
using namespace std;
// function that calculate unique element
int unique(vector<vector<int>> &mat) {
// declare map for hashing
int n = mat.size();
int m = mat[0].size();
unordered_map<int, int> mp;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
// increase freq of mat[i][j] in map
mp[(mat[i][j])]++;
int flag = false;
for (auto p : mp) {
if (p.second == 1) {
cout << p.first << " ";
flag = 1;
}
}
if (!flag) {
cout << "No unique element in the matrix";
}
}
int main() {
vector<vector<int>> mat = {{1, 2, 3, 20},
{5, 6, 20, 25},
{1, 3, 5, 6},
{6, 7, 8, 15}};
unique(mat);
return 0;
}
Java
// Java program to find unique element in matrix
import java.util.*;
class GfG {
// Function that calculates unique element
static void unique(int[][] mat) {
// Declare map for hashing
int n = mat.length;
int m = mat[0].length;
Map<Integer, Integer> mp = new HashMap<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// Increase freq of mat[i][j] in map
mp.put(mat[i][j],
mp.getOrDefault(mat[i][j], 0) + 1);
}
}
boolean flag = false;
// Print unique element
for (Map.Entry<Integer, Integer> entry :
mp.entrySet()) {
if (entry.getValue() == 1) {
System.out.print(entry.getKey() + " ");
flag = true;
}
}
if (!flag) {
System.out.println(
"No unique element in the matrix");
}
}
public static void main(String[] args) {
int[][] mat = { { 1, 2, 3, 20 },
{ 5, 6, 20, 25 },
{ 1, 3, 5, 6 },
{ 6, 7, 8, 15 } };
unique(mat);
}
}
Python
# Python program to find unique element in matrix
# Function that calculates unique element
def unique(mat):
# Declare map for hashing
n = len(mat)
m = len(mat[0])
mp = {}
for i in range(n):
for j in range(m):
# Increase freq of mat[i][j] in map
mp[mat[i][j]] = mp.get(mat[i][j], 0) + 1
flag = False
# Print unique element
for key, value in mp.items():
if value == 1:
print(key, end=" ")
flag = True
if not flag:
print("No unique element in the matrix")
if __name__ == "__main__":
mat = [
[1, 2, 3, 20],
[5, 6, 20, 25],
[1, 3, 5, 6],
[6, 7, 8, 15]
]
unique(mat)
C#
// C# program to find unique element in matrix
using System;
using System.Collections.Generic;
class GfG {
// Function that calculates unique element
static void Unique(int[, ] mat) {
// Declare map for hashing
int n = mat.GetLength(0);
int m = mat.GetLength(1);
Dictionary<int, int> mp
= new Dictionary<int, int>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// Increase freq of mat[i, j] in map
if (mp.ContainsKey(mat[i, j])) {
mp[mat[i, j]]++;
}
else {
mp[mat[i, j]] = 1;
}
}
}
bool flag = false;
// Print unique element
foreach(var p in mp) {
if (p.Value == 1) {
Console.Write(p.Key + " ");
flag = true;
}
}
if (!flag) {
Console.WriteLine(
"No unique element in the matrix");
}
}
public static void Main(string[] args) {
int[, ] mat = { { 1, 2, 3, 20 },
{ 5, 6, 20, 25 },
{ 1, 3, 5, 6 },
{ 6, 7, 8, 15 } };
Unique(mat);
}
}
JavaScript
// Javascript program to find unique element in matrix
// function that calculates unique elements
function unique(mat, n, m) {
// declare map for hashing
const mp = new Map();
// Loop through the matrix to store
// frequency of each element
for (var i = 0; i < n; i++) {
for (var j = 0; j < m; j++) {
// increase frequency of mat[i][j] in map
if (mp.has(mat[i][j])) {
mp.set(mat[i][j], 1 + mp.get(mat[i][j]));
} else {
mp.set(mat[i][j], 1);
}
}
}
var flag = false;
// Print unique elements
for (const [key, value] of mp) {
if (value == 1) {
console.log(`${key}`);
flag = true;
}
}
if (!flag) {
console.log("No unique element in the matrix");
}
}
var mat = [
[1, 2, 3, 20],
[5, 6, 20, 25],
[1, 3, 5, 6],
[6, 7, 8, 15]
];
var R = mat.length;
var C = mat[0].length;
unique(mat, R, C);
Time Complexity: O(n*m)
Auxiliary Space: O(n*m)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem