Replace Elements in an Array

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

It is guaranteed that in the i^th operation:

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

Return the array obtained after applying all the operations.

Example 1
Inputnums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
Output[3,2,7,1]
After replacing 1 with 3, 4 with 7, and 6 with 1, the final array is [3, 2, 7, 1].
Example 2
Inputnums = [1,2], operations = [[1,3],[2,1],[3,2]]
Output[2,1]
After replacing 1 with 3, 2 with 1, and 3 with 2, the final array is [2, 1].

Constraints

  • n == nums.length
  • m == operations.length
  • 1 <= n, m <= 10^5
  • All the values of nums are distinct.
  • operations[i].length == 2
  • 1 <= nums[i], operations[i][0], operations[i][1] <= 10^6
  • operations[i][0] will exist in nums when applying the i^th operation.
  • operations[i][1] will not exist in nums when applying the i^th operation.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate