Given a matrix mat[][] having n rows and m columns. We need to find unique elements in matrix i.e, those elements which are not repeated in the matrix or those elements whose frequency is 1.
Examples:
Input : 20 15 30 2
2 3 5 30
6 7 6 8
Output : 3 20 5 7 8 15
Input : 1 2 3
5 6 2
1 3 5
6 2 2
Output : No unique element in the matrix
Follow these steps to find a unique element:
- Create an empty hash table or dictionary.
- Traverse through all the elements of the matrix
- If element is present in the dictionary, then increment its count
- Otherwise insert element with value = 1.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
int unique( int mat[R][C], int n, int m)
{
int maximum = 0, flag = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m; j++)
if (maximum < mat[i][j])
maximum = mat[i][j];
int b[maximum + 1] = {0};
for ( int i = 0 ; i < n; i++)
for ( int j = 0; j < m; j++)
b[(mat[i][j])]++;
for ( int i = 1; i <= maximum; i++)
if (b[i] == 1)
cout << i << " " ;
flag = 1;
if (!flag){
cout << "No unique element in the matrix" ;
}
}
int main()
{
int mat[R][C] = {{ 1, 2, 3, 20},
{5, 6, 20, 25},
{1, 3, 5, 6},
{6, 7, 8, 15}};
unique(mat, R, C);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int R = 4 , C = 4 ;
static void unique( int mat[][],
int n, int m)
{
int maximum = 0 , flag = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < m; j++)
if (maximum < mat[i][j])
maximum = mat[i][j];
int b[] = new int [maximum + 1 ];
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < m; j++)
b[(mat[i][j])]++;
for ( int i = 1 ; i <= maximum; i++)
if (b[i] == 1 )
System.out.print(i + " " );
flag = 1 ;
if (flag == 0 )
{
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, R, C);
}
}
|
Python3
def unique(mat, n, m):
maximum = 0 ; flag = 0
for i in range ( 0 , n):
for j in range ( 0 , m):
if (maximum < mat[i][j]):
maximum = mat[i][j];
uniqueElementDict = [ 0 ] * (maximum + 1 )
for i in range ( 0 , n):
for j in range ( 0 , m):
uniqueElementDict[(mat[i][j])] + = 1
for key in range (maximum + 1 ):
if uniqueElementDict[key] = = 1 :
print (key, end = " " )
flag = 1
if (flag = = 0 ):
print ( "No unique element in the matrix" )
mat = [[ 1 , 2 , 3 , 20 ],
[ 5 , 6 , 20 , 25 ],
[ 1 , 3 , 5 , 6 ],
[ 6 , 7 , 8 , 15 ]]
n = 4
m = 4
unique(mat, n, m)
|
C#
using System;
class GFG
{
static int R = 4, C = 4;
static void unique( int [,]mat,
int n, int m)
{
int maximum = 0, flag = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m; j++)
if (maximum < mat[i, j])
maximum = mat[i, j];
int []b = new int [maximum + 1];
for ( int i = 0 ; i < n; i++)
for ( int j = 0; j < m; j++)
b[(mat[i, j])]++;
for ( int i = 1; i <= maximum; i++)
if (b[i] == 1)
Console.Write(i + " " );
flag = 1;
if (flag == 0)
{
Console.WriteLine( "No unique element " +
"in the matrix" );
}
}
public static void Main()
{
int [,]mat = {{1, 2, 3, 20},
{5, 6, 20, 25},
{1, 3, 5, 6},
{6, 7, 8, 15}};
unique(mat, R, C);
}
}
|
PHP
<?php
$R = 4;
$C = 4;
function unique( $mat , $n , $m )
{
$maximum = 0;
$flag = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $m ; $j ++)
if ( $maximum < $mat [ $i ][ $j ])
$maximum = $mat [ $i ][ $j ];
$b = array ();
for ( $j = 0; $j < $maximum + 1; $j ++)
$b [ $j ] = 0;
for ( $i = 0 ; $i < $n ; $i ++)
for ( $j = 0; $j < $m ; $j ++)
$b [ $mat [ $i ][ $j ]]++;
for ( $i = 1; $i <= $maximum ; $i ++)
if ( $b [ $i ] == 1)
echo "$i " ;
$flag = 1;
if (! $flag )
{
echo "No unique element in the matrix" ;
}
}
$mat = array ( array (1, 2, 3, 20),
array (5, 6, 20, 25),
array (1, 3, 5, 6),
array (6, 7, 8, 15));
unique( $mat , $R , $C );
?>
|
Javascript
<script>
var R = 4, C = 4;
function unique(mat, n, m)
{
var maximum = 0, flag = 0;
for ( var i = 0; i < n; i++)
for ( var j = 0; j < m; j++)
if (maximum < mat[i][j])
maximum = mat[i][j];
var b = Array(maximum+1).fill(0);
for ( var i = 0 ; i < n; i++)
for ( var j = 0; j < m; j++)
b[(mat[i][j])]++;
for ( var i = 1; i <= maximum; i++)
if (b[i] == 1)
document.write(i + " " );
flag = 1;
if (flag == 0)
{
document.write( "No unique element " +
"in the matrix<br>" );
}
}
var mat = [ [ 1, 2, 3, 20 ],
[ 5, 6, 20, 25 ],
[ 1, 3, 5, 6 ],
[ 6, 7, 8, 15 ] ];
unique(mat, R, C);
</script>
|
Complexity Analysis:
- Time Complexity: O(m*n) where m is the number of rows & n is the number of columns.
- Auxiliary Space: O(max(matrix)).
Method – 2: Using HashMap
This approach uses a hashmap instead of creating a hashtable of size max element + 1.
Implementation
C++
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
int unique( int mat[R][C], int n, int m)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m; j++)
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()
{
int mat[R][C] = { { 1, 2, 3, 20 },
{ 5, 6, 20, 25 },
{ 1, 3, 5, 6 },
{ 6, 7, 8, 15 } };
unique(mat, R, C);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void unique( int mat[][], int R, int C) {
Map<Integer, Integer> map = new HashMap<>();
for ( int i = 0 ; i < R; i++) {
for ( int j = 0 ; j < C; j++) {
if (map.containsKey(mat[i][j])) {
map.put(mat[i][j], 1 + map.get(mat[i][j]));
} else {
map.put(mat[i][j], 1 );
}
}
}
int flag = 0 ;
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
if (e.getValue() == 1 ) {
System.out.print(e.getKey() + " " );
flag = 1 ;
}
}
if (flag == 0 ) {
System.out.println( "No unique element in the matrix" );
}
}
public static void main(String[] args) {
int R = 4 ;
int C = 4 ;
int mat[][] = { { 1 , 2 , 3 , 20 },
{ 5 , 6 , 20 , 25 },
{ 1 , 3 , 5 , 6 },
{ 6 , 7 , 8 , 15 }
};
unique(mat, R, C);
}
}
|
Python3
def unique(mat, r, c) - > int :
mp = {}
for i in range (r):
for j in range (c):
if mat[i][j] not in mp:
mp[(mat[i][j])] = 1
else :
mp[(mat[i][j])] + = 1
flag = False
for p in mp:
if mp[p] = = 1 :
print (p, end = " " )
flag = True
if flag = = False :
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, 4 , 4 )
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static void unique( int [, ] mat, int R, int C)
{
Dictionary< int , int > map = new Dictionary< int , int >();
for ( int i = 0; i < R; i++) {
for ( int j = 0; j < C; j++) {
if (map.ContainsKey(mat[i, j])) {
map[(mat[i, j])] = 1 + map[(mat[i, j])];
}
else {
map[(mat[i, j])] = 1;
}
}
}
int flag = 0;
foreach (KeyValuePair< int , int > e in map)
{
if (e.Value == 1)
{
Console.Write(e.Key + " " );
flag = 1;
}
}
if (flag == 0) {
Console.WriteLine(
"No unique element in the matrix" );
}
}
public static void Main( string [] args)
{
int R = 4;
int C = 4;
int [, ] mat = { { 1, 2, 3, 20 },
{ 5, 6, 20, 25 },
{ 1, 3, 5, 6 },
{ 6, 7, 8, 15 } };
unique(mat, R, C);
}
}
|
Javascript
var R = 4;
var C = 4;
function unique(mat, n, m) {
const mp = new Map();
for ( var i = 0; i < n; i++) {
for ( var j = 0; j < m; j++) {
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 ;
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]
];
unique(mat, R, C);
|
Time Complexity: O(R*C)
Auxiliary Space: O(R*C)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
24 Mar, 2023
Like Article
Save Article