You are given a 0-indexed array nums that consists of n distinct positive integers. Apply m operations to this array, where in the ith operation you replace the number operations[i][0] with operations[i][1].

It is guaranteed that in the ith operation:


	operations[i][0] exists in nums.
	operations[i][1] does not exist in nums.


Return the array obtained after applying all the operations.

&nbsp;
Example 1:

Input: nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
Output: [3,2,7,1]
Explanation: We perform the following operations on nums:
- Replace the number 1 with 3. nums becomes [3,2,4,6].
- Replace the number 4 with 7. nums becomes [3,2,7,6].
- Replace the number 6 with 1. nums becomes [3,2,7,1].
We return the final array [3,2,7,1].


Example 2:

Input: nums = [1,2], operations = [[1,3],[2,1],[3,2]]
Output: [2,1]
Explanation: We perform the following operations to nums:
- Replace the number 1 with 3. nums becomes [3,2].
- Replace the number 2 with 1. nums becomes [3,1].
- Replace the number 3 with 2. nums becomes [2,1].
We return the array [2,1].


&nbsp;
Constraints:


	n == nums.length
	m == operations.length
	1 &lt;= n, m &lt;= 105
	All the values of nums are distinct.
	operations[i].length == 2
	1 &lt;= nums[i], operations[i][0], operations[i][1] &lt;= 106
	operations[i][0] will exist in nums when applying the ith operation.
	operations[i][1] will not exist in nums when applying the ith operation.


class Solution:
    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
            replacements = {}
            for x, y in reversed(operations):
                replacements[x] = replacements.get(y, y)
            for idx, val in enumerate(nums):
                if val in replacements:
                    nums[idx] = replacements[val]
            return nums
class Solution {
    public int[] arrayChange(int[] nums, int[][] operations) {
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<nums.length;i++) map.put(nums[i],i);
        for(int[] op: operations) {
            nums[map.get(op[0])] = op[1];
            map.put(op[1],map.get(op[0]));
            map.remove(op[0]);
        }
        return nums;
    }
}
int m[1000001];
class Solution {
public:
vector<int> arrayChange(vector<int>& nums, vector<vector<int>>& operations) {
    for (int i = 0; i < nums.size(); ++i)
        m[nums[i]] = i;
    for (auto &op : operations) {
        nums[m[op[0]]] = op[1];
        m[op[1]] = m[op[0]];
    }
    return nums;
}
};
/**
 * @param {number[]} nums
 * @param {number[][]} operations
 * @return {number[]}
 */
var arrayChange = function(nums, operations) {
    let map = new Map()

    for(let i = 0; i < nums.length; i++){
        let num = nums[i]
        map.set(num, i)
    }

    for(let op of operations){
        let key = op[0]
        let value = op[1]

        // if key exists
        if(map.has(key)){
            const idx = map.get(key)
            map.set(value, idx)
            map.delete(key)
        }
    }

    for(let [key, idx] of map){
        nums[idx] = key
    }

    return nums

};
Replace Elements in an Array