Skip to content

2295. Replace Elements in an Array

Leetcode - 2295. Replace Elements in an Array

Submission

class Solution {
    public int[] arrayChange(int[] nums, int[][] operations) {
        Map<Integer, Integer> m = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            m.put(nums[i], i);
        }

        for (int[] operation : operations) {
            int position = m.get(operation[0]);
            m.put(operation[1], m.get(operation[0]));
            m.remove(operation[0]);
            nums[position] = operation[1];
        }

        return nums;
    }
}