


1. The output array can contain the elements in any particular order.
2. Even if a particular element appears more than once in each matrix row, it should still be present only once in the output array.
Consider the matrix MAT [ [ 2, 3, 4, 7 ] , [ 0, 0, 3, 5 ] , [ 1, 3, 8, 9 ] ] having 3 rows and 4 columns respectively.
The output array should be [ 3 ] as 3 is the only element that is present in all three rows.
The first line of the input contains an integer, 'T', denoting the number of test cases.
The first line of each test case contains two space-separated integers, 'N' and 'M', denoting the number of rows and columns in the Matrix 'MAT', respectively.
Each of the next 'N' lines contains 'M' space-separated integers denoting the matrix 'MAT' elements.
The only line of output of each test case should contain all elements which are present in each row of the given matrix separated by a single space.
Print the output of each test case in a new line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N, M <= 1000
0 <= MAT[i][j] <= 10^9
Where 'MAT[i][j]' denotes the element present at the 'i-th' row and the 'j-th' column of the matrix 'MAT'.
Time Limit: 1 sec
The idea is to iterate through the first row, and for each element of the row, iterate through all the other rows and check whether that element is present in them. If that element exists in all the other rows, we will add it to the output array. In the end, we will return the output array after removing duplicates.
Algorithm
The idea is to sort all the matrix rows and then maintain one pointer for each row. Let the array that stores the pointer to the column number currently being pointed for each row be currentColumn with all values initialised to 0. In each iteration, we first find the element which is currently pointed by the pointer to the first row, i.e., MAT[0][currentColumn[0]]. Then we move the pointers to all the other rows ahead while the element pointed by them is smaller than the element pointed in the first row. Then we check whether all the pointers are pointing to the same value. If yes, then we add that value to the output array provided it already does not exist in it and move the pointer to the first row ahead.
Algorithm
The idea is to use a HashMap to store the elements present in all the rows of the matrix. The advantage of using a HashMap is that it can search for an element present in the HashMap in constant time. Let commonElements be a HashMap having the matrix element as its key, and its corresponding value will be the maximum number of consecutive rows starting from the first row in which that element is present. We will first add all the first row elements in the HashMap and set their value as 1. Then we will traverse the matrix, and whenever we find an element that is present in all the previous rows, we will increase its corresponding value in the HashMap by 1 because that element was already present in the previous rows, and now we were able to find it in the current row. Therefore it is present in all the rows that have been traversed till now. In the end, we will iterate the commonElements HashMap and add all those keys to the output array whose corresponding value is equal to the number of rows of the matrix.
Algorithm