Replace Elements in an Array LeetCode Solution

Problem – Replace Elements in an Array LeetCode Solution

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.

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].

Constraints:

  • n == nums.length
  • m == operations.length
  • 1 <= n, m <= 105
  • All the values of nums are distinct.
  • operations[i].length == 2
  • 1 <= nums[i], operations[i][0], operations[i][1] <= 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.

Replace Elements in an Array LeetCode Solution in C++

class Solution {
public:
    vector<int> arrayChange(vector<int>& nums, vector<vector<int>>& operations) {
        
        unordered_map<int,int> hm;     //****store index of element in map
        for(int i =0;i<nums.size();i++){
            
            hm[nums[i]] = i;   //** use map to find the index 
        }
        
        for(int i =0;i<operations.size();i++){
            
            
            int a  = operations[i][0];
            int b = operations[i][1];
            
            nums[hm[a]] = b;
            
            hm[b] = hm[a];  //**also store the changed element in hashmap
            
            
        }
        return nums;
        
    }
};

Replace Elements in an Array LeetCode Solution in Java

public int[] arrayChange(int[] A, int[][] op) {
    HashMap<Integer,Integer> store= new HashMap<>();
    for(int i=0;i<A.length;i++) store.put(A[i],i);
    for(var i:op){
        A[store.get(i[0])]= i[1];              //replace value to its index
        store.put(i[1],store.get(i[0]));       //update new value with its index
    }
    return A;
}

Replace Elements in an Array LeetCode Solution in Python

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
Replace Elements in an Array LeetCode Solution Review:

In our experience, we suggest you solve this Replace Elements in an Array LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Replace Elements in an Array LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Replace Elements in an Array LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.