C++ program to remove row or column wise duplicates from matrix of characters


We are given a 2D matrix with rows and columns. The matrix consists of elements in char data type. A method is devised to remove the elements which are duplicated in their respective rows or columns.

In this method, we check if any element is repeating in its row or column for each character. If it is not repeated, we leave it as it was before.

We can store the values occurring in each row and column in a map. After which, we can traverse again and take those values which are appearing only once in their row and column. This would require extra space, and we can do a space-time tradeoff by searching for each element in its row and column. We have used a map to store the character and the count.

vector<vector<char>> arr = {
   {'A', 'B', 'E', 'G', 'L'},
   {'K', 'M', 'P', 'Q', 'S'},
   {'V', 'W', 'T', 'B', 'O'},
   {'K', 'H', 'O', 'U', 'P'},
   {'P', 'I', 'C', 'Y', 'J'},
   {'F', 'S', 'K', 'E', 'W'},
   {'N', 'L', 'Y', 'G', 'O'},
};
res = solve(arr);

Example (Using Vector ADT)

Suppose we have a matrix with the following characters −

A C E G I
K M O Q S
U W Y B D
K H I N P
R T W X Z
A E I M Q
L O P G A

Here, for the first row of the matrix, we cannot consider ‘A’ and ‘G’ because they occur twice in their respective columns, but we can consider ‘C’, ‘E’, ‘I’. Following is a C++ program for the above example −

#include <iostream> #include <vector> #include <map> using namespace std; void solve(vector<vector<char>>& arr) { vector<map<char, int>> rows(arr.size()), columns(arr[0].size()); for(int i=0;i<arr.size();i++) { for(int j=0;j<arr[i].size();j++) { rows[i][arr[i][j]]++; columns[j][arr[i][j]]++; } } for(int i=0;i<arr.size();i++) { for(int j=0;j<arr[i].size();j++) { if(rows[i][arr[i][j]] == 1 && columns[j][arr[i][j]] == 1) { cout << arr[i][j]; } } } } int main() { vector<vector<char>> arr = { {'A', 'C', 'E', 'G', 'I'}, {'K', 'M', 'O', 'Q', 'S'}, {'U', 'W', 'Y', 'B', 'D'}, {'K', 'H', 'I', 'N', 'P'}, {'R', 'T', 'W', 'X', 'Z'}, {'A', 'E', 'I', 'M', 'Q'}, {'L', 'O', 'P', 'G', 'A'}, }; solve(arr); return 0; }

Output

CEIMOQSUWYBDHNPRTWXZEMQLOPA

Example (Without Using Vector ADT)

#include <bits/stdc++.h> using namespace std; int main() { int row = 2, column = 8; const char* input_array[] = { "ABCDPQST", "ECFDOMUT" }; bool boolean_array[row][column]; memset(boolean_array, 0, sizeof(boolean_array)); for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { for (int k = 0; k < column; k++) { if (input_array[i][j] == input_array[i][k] && j != k) { boolean_array[i][j] = true; boolean_array[i][k] = true; } } for (int k = 0; k < row; k++) { if (input_array[i][j] == input_array[k][j] && i != k) { boolean_array[i][j] = true; boolean_array[k][j] = true; } } } } for (int i = 0; i < row; i++) for (int j = 0; j < column; j++) if (!boolean_array[i][j]) printf("%c ", input_array[i][j]); return 0; }

Output

A B C P Q S E C F O M U

Conclusion

We have used O(n^2) complexity. The log(n) complexity of the map can be ignored for only lower case Latin characters as there are 26 characters only. But for a character of more random characters, this might come into play. You can try solving this problem without using hashing and extra space for yourself.

Updated on: 10-Aug-2022

354 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements