Norway


#include <iostream>

#include <unordered_map>

using namespace std;

 

#define 8

#define COL 6

 

// all in every row

// of given

int findCommonElements(int mat[ROW][COL])

{

    // create an empty map

    unordered_map<int, int> map;

 

    // insert all elements of the first row in the map

    // with their value set as 1

    for (int c = 0; c < COL; c++) {

        map.insert(pair<int, int>(mat[0][c], 1));

    }

 

    // do for remaining rows

    for (int r = 1; r < ROW; r++)

    {

        for (int c = 0; c < COL; c++)

        {

            // get current element

            int curr = mat[r][c];

            

            // if current element is present in the map and its value

            // is same as row index, increment its value by 1

            // This check also handles duplicate entries in same row

            if (map.find(curr) != map.end() && map[curr] == r) {

                map[curr] = r + 1;

            }

        }

    }

 

    // iterate over each entry in map and print keys having

    // their value equal to ROW (number of rows in the matrix)

    cout << “Common elements are: “;

    for (auto it : map) {

        if (it.second == ROW) {

            cout << it.first << ” “;

        }

    }

}

 

// main function

int main()

{

    int mat[ROW][COL] =

    {

        { 7, 1, 3, 5, 3, 6 },

        { 2, 3, 6, 1, 1, 6 },

        { 6, 1, 7, 2, 1, 4 },

        { 6, 6, 7, 1, 3, 3 },

        { 5, 5, 6, 1, 5, 4 },

        { 3, 5, 6, 2, 7, 1 },

        { 4, 1, 4, 3, 6, 4 },

        { 4, 6, 1, 7, 4, 3 }

    };

 

    findCommonElements(mat);

 

    return 0;

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here