# Map anagrams to one another in JavaScript

## Anagram arrays:

One array is an anagram of another if we can randomise the elements of that array to achieve the other array.

For example −

[1, 2, 3] and [2, 1, 3] are anagrams of each other.

Suppose, we have two arrays, arr1 and arr2 which are anagrams of each other.

We are required to write a JavaScript function that takes in these two arrays and returns a new mapping array of the same length as arr1 and arr2. The mapping array should contain the index of elements of the arr1 array as they are present in the arr2 array.

For example −

If the two input arrays are −

const arr1 = [23, 39, 57, 43, 61];
const arr2 = [61, 23, 43, 57, 39];

Then the output should be −

const output = [1, 4, 3, 2, 0];

because the item at index 0 in arr1 is at index 1 in arr2

item at index 1 in arr1 is at index 4 in arr2 and so on

## Example

The code for this will be −

Live Demo

const arr1 = [23, 39, 57, 43, 61];
const arr2 = [61, 23, 43, 57, 39];
const anagramMappings = (arr1 = [], arr2 = []) => {
const res = [];
for(let i = 0; i < arr1.length; i++) {
for(let j = 0; j < arr2.length; j++) {
if(arr1[i] == arr2[j]){
res.push(j);
};
};
};
return res;
};
console.log(anagramMappings(arr1, arr2));

## Output

And the output in the console will be −

[ 1, 4, 3, 2, 0 ]
{"ft_lang_label":"__label__en","ft_lang_prob":0.7146075,"math_prob":0.9919251,"size":2546,"snap":"2022-40-2023-06","text_gpt3_token_len":715,"char_repetition_ratio":0.15420929,"word_repetition_ratio":0.061728396,"special_character_ratio":0.30400628,"punctuation_ratio":0.13513513,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98713547,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-31T12:59:11Z\",\"WARC-Record-ID\":\"<urn:uuid:54e30e41-688e-4e01-b323-2c239cea3313>\",\"Content-Length\":\"41993\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:968007f2-5882-430c-ae42-d47740182bc1>\",\"WARC-Concurrent-To\":\"<urn:uuid:6f836b97-bbcf-404e-9466-a2cd0fed8bfc>\",\"WARC-IP-Address\":\"192.229.210.176\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/map-anagrams-to-one-another-in-javascript\",\"WARC-Payload-Digest\":\"sha1:6KVTJIJCB44LQD247TFGL5C4J6UXAHAY\",\"WARC-Block-Digest\":\"sha1:SPP2D75XICFUL5CUSPX7AZHTYEWX5VRN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499871.68_warc_CC-MAIN-20230131122916-20230131152916-00300.warc.gz\"}"}