Given two arrays *A* and *B* of positive integers, elements of array *B* can be mapped to elements of array *A* only if both the elements have same value. The task is to compute the positions in array *A* to which elements of array *B* will be mapped. Print NA if mapping for a particular element cannot be done.

**Note:** For one position only one integer can be mapped.

**Examples:**

Input:A[] = {1, 5, 2, 4, 4, 3}, B[] = {1, 2, 5, 1}

Output:0 2 1 NA

B[0], B[1] and B[2] can be mapped to A[0], A[2] and A[1] respectively but B[3] cannot be mapped to any element of A because the only ‘1’ in A has already been mapped

Input:A[] = {2, 1, 2, 3, 3, 4, 2, 4, 1}, B[] = {1, 2, 5, 1, 2, 4, 2, 3, 2, 1}

Output:1 0 NA 8 2 5 6 3 NA NA

The idea is to use a hash table where keys are elements of A[] and values are indexes of these elements. Since there can be more than one occurrences of an element, we use a list of items as values in the hash table.

Below is the implementation of the above problem:

`// C++ program to map elements of an array ` `// to equal elements of another array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the mapping of elements ` `void` `printMapping(` `int` `A[], ` `int` `B[], ` `int` `N, ` `int` `M) ` `{ ` ` ` ` ` `// Create a hash table where all indexes are ` ` ` `// stored for a given value ` ` ` `unordered_map<` `int` `, list<` `int` `>> m; ` ` ` `for` `(` `int` `i=0; i<N; i++) ` ` ` `m[A[i]].push_back(i); ` ` ` ` ` `// Traverse through B[] ` ` ` `for` `(` `int` `i=0; i<M; i++) ` ` ` `{ ` ` ` `// If a mapping is found, print the mapping and ` ` ` `// remove the index from hash table so that the ` ` ` `// same element of A[] is not mapped again. ` ` ` `if` `(m.find(B[i]) != m.end() && m[B[i]].size() > 0) ` ` ` `{ ` ` ` `cout << m[B[i]].front() << ` `" "` `; ` ` ` `m[B[i]].pop_front(); ` ` ` `} ` ` ` ` ` `else` `// No mapping found ` ` ` `{ ` ` ` `cout << ` `"NA "` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `A[] = {2, 1, 2, 3, 3, 4, 2, 4, 1}; ` ` ` `int` `N = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` `int` `B[] = {1, 2, 5, 1, 2, 4, 2, 3, 2, 1}; ` ` ` `int` `M = ` `sizeof` `(B) / ` `sizeof` `(B[0]); ` ` ` `printMapping(A, B, N, M); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 0 NA 8 2 5 6 3 NA NA

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find whether an array is subset of another array using Map
- Check if Array elements can be maximized upto M by adding all elements from another array
- Elements of an array that are not divisible by any element of another array
- Sum of elements in an array whose difference with the mean of another array is less than k
- Print elements of an array according to the order defined by another array | set 2
- Sort an array by swapping elements of different type specified by another array
- Maximize Array sum by swapping at most K elements with another array
- Check if each element of an Array is the Sum of any two elements of another Array
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Rearrange an array to make similar indexed elements different from that of another array
- Maximize elements using another array
- Count elements that are divisible by at-least one element in another array
- Convert one array to another using adjacent swaps of elements
- Sort elements by frequency | Set 5 (using Java Map)
- Merge an array of size n into another array of size m+n
- Find whether an array is subset of another array | Added Method 3
- Sort an array according to the order defined by another array
- Maximum possible XOR of every element in an array with another array
- Closest greater element for every array element from another array
- Maximum in an array that can make another array sorted

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.