Find Anagram Mappings - Problem

You are given two integer arrays nums1 and nums2 where nums2 is an anagram of nums1. Both arrays may contain duplicates.

Return an index mapping array mapping from nums1 to nums2 where mapping[i] = j means the ith element in nums1 appears in nums2 at index j.

If there are multiple answers, return any of them. An array a is an anagram of an array b means b is made by randomizing the order of the elements in a.

Input & Output

Example 1 — Basic Mapping
$ Input: nums1 = [12,28,46,32,50], nums2 = [50,12,32,46,28]
Output: [1,4,3,2,0]
💡 Note: 12 maps to index 1, 28 maps to index 4, 46 maps to index 3, 32 maps to index 2, 50 maps to index 0
Example 2 — With Duplicates
$ Input: nums1 = [84,46], nums2 = [84,46]
Output: [0,1]
💡 Note: nums1[0]=84 maps to nums2[0]=84, nums1[1]=46 maps to nums2[1]=46
Example 3 — Multiple Same Values
$ Input: nums1 = [40,40,40], nums2 = [40,40,40]
Output: [0,1,2]
💡 Note: First 40 maps to index 0, second 40 maps to index 1, third 40 maps to index 2

Constraints

  • 1 ≤ nums1.length ≤ 100
  • nums2.length == nums1.length
  • -105 ≤ nums1[i], nums2[i] ≤ 105
  • nums2 is an anagram of nums1

Visualization

Tap to expand
Find Anagram Mappings122846320123nums1461232280123nums2 (anagram)Output Mapping:[1, 3, 0, 2]
Understanding the Visualization
1
Input Arrays
nums1 = [12,28,46,32] and nums2 = [46,12,32,28] (anagram)
2
Find Mapping
For each nums1[i], find where that value appears in nums2
3
Output Indices
Return array [1,3,0,2] showing the index mappings
Key Takeaway
🎯 Key Insight: Build a value-to-indices mapping for instant lookups instead of searching linearly
Asked in
Google 25 Amazon 18 Facebook 12
32.5K Views
Medium Frequency
~15 min Avg. Time
684 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen