Map elements of an array to elements of another array
Last Updated :
08 May, 2023
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:
Implementation:
CPP
// 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;
}
Java
/*package whatever //do not write package name here */
import java.util.*;
class GFG {
// Function to print the mapping of elements
static void printMapping(int A[], int B[], int N, int M)
{
// Create a hash table where all indexes are
// stored for a given value
HashMap<Integer,List<Integer>> m = new HashMap<>();
for(int i:A){
m.put(i,new ArrayList<Integer>());
}
for (int i=0; i<N; i++)
m.get(A[i]).add(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.containsKey(B[i]) && m.get(B[i]).size() > 0)
{
System.out.print(m.get(B[i]).get(0) + " ");
m.get(B[i]).remove(0);
}
else // No mapping found
{
System.out.print("NA" + " ");
}
}
}
public static void main (String[] args) {
int A[] = {2, 1, 2, 3, 3, 4, 2, 4, 1};
int N = A.length;
int B[] = {1, 2, 5, 1, 2, 4, 2, 3, 2, 1};
int M = B.length;
printMapping(A, B, N, M);
}
}
Python3
# Python3 program to map elements of an array
# to equal elements of another array
# Function to print the mapping of elements
def printMapping(A, B):
# Create a hash table where all indexes are
# stored for a given value
m = {}
for i in range(len(A)):
if A[i] in m:
m[A[i]].append(i)
else:
m[A[i]] = [i]
# Traverse through B[]
for i in range(len(B)):
# 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 B[i] in m and len(m[B[i]]) > 0:
print(m[B[i]].pop(0),end=" ")
else:
print("NA",end=" ")
# Driver code
A = [2, 1, 2, 3, 3, 4, 2, 4, 1]
B = [1, 2, 5, 1, 2, 4, 2, 3, 2, 1]
printMapping(A, B)
# This code is contributed by akashish__
C#
using System;
using System.Collections.Generic;
public class GFG {
// Function to print the mapping of elements
static void printMapping(int[] A, int[] B, int N, int M)
{
// Create a hash table where all indexes are
// stored for a given value
Dictionary<int, List<int> > m
= new Dictionary<int, List<int> >();
for (int i = 0; i < A.Length; i++) {
if (!m.ContainsKey(A[i]))
m.Add(A[i], new List<int>());
}
for (int i = 0; i < N; i++)
m[A[i]].Add(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.ContainsKey(B[i]) && m[B[i]].Count > 0) {
Console.Write(m[B[i]][0] + " ");
m[B[i]].RemoveAt(0);
}
else // No mapping found
{
Console.Write("NA"
+ " ");
}
}
}
static public void Main()
{
int[] A = { 2, 1, 2, 3, 3, 4, 2, 4, 1 };
int N = A.Length;
int[] B = { 1, 2, 5, 1, 2, 4, 2, 3, 2, 1 };
int M = B.Length;
printMapping(A, B, N, M);
}
}
// This code is contributed by akashish__
JavaScript
// JavaScript program to map elements of an array
// to equal elements of another array
// Function to print the mapping of elements
function printMapping(A, B)
{
// Create a hash table where all indexes are
// stored for a given value
let m = new Map();
for (let i = 0; i < A.length; i++) {
if (m.has(A[i])) {
m.get(A[i]).push(i);
} else {
m.set(A[i], [i]);
}
}
// Traverse through B[]
for (let i = 0; i < B.length; 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.has(B[i]) && m.get(B[i]).length > 0) {
console.log(m.get(B[i]).shift());
} else {
console.log("NA");
}
}
}
// Driver code
let A = [2, 1, 2, 3, 3, 4, 2, 4, 1];
let B = [1, 2, 5, 1, 2, 4, 2, 3, 2, 1];
printMapping(A, B);
// This code is contributed by akashish__
Output1 0 NA 8 2 5 6 3 NA NA
Time Complexity: O(N+M)
Auxiliary Space: O(N)
Similar Reads
Find whether an array is subset of another array using Map Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both arrays are distinct.Examples: Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1} Output: arr2[] is a subset o
7 min read
JavaScript - Match Values from Another Array and Assign to Object of Arrays Here are the different methods to match values from another array and assign to object of arrays in JavaScript1. Using forEach() and push()In this method, we will be using the forEach() and push() methods of the array to get the same value from another array and assign it to the object of arrays.Jav
3 min read
For each element in 1st array count elements less than or equal to it in 2nd array | Set 2 Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[]. Examples: Input : arr1[] = [1, 2, 3, 4, 7, 9] arr2[] = [0, 1, 2, 1, 1, 4] Output : [4, 5, 5, 6, 6, 6] Explanation: There are 4 elements less t
13 min read
Merge two Maps of Array into one sorted Map of Array Given two maps map1 and map2 having a string as the key and arrays of integers as values, the task is to merge them in one map such that if a key is common in both the maps, the respective arrays should be merged. Examples: Input: map1 = { ("key1", {0, 1}), ("key2", {0, 1}) }, map2 = { ("key2", {1,
12 min read
Count of possible unique arrays after swapping elements at same index of given Arrays Given two arrays arr1[] and arr2[] with distinct elements of size N.The task is to count the total number of possible combinations after swapping elements at the same index of both the arrays such that there are no duplicates in both the arrays after performing the operation. Examples: Input: arr1[]
12 min read