Skip to content

Commit 835a30a

Browse files
authored
Create Replace Elements in an Array.java
1 parent 5da9421 commit 835a30a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int[] arrayChange(int[] nums, int[][] operations) {
3+
Map<Integer, Integer> indexMap = new HashMap<>();
4+
for (int i = 0; i < nums.length; i++) {
5+
indexMap.put(nums[i], i);
6+
}
7+
for (int[] operation : operations) {
8+
int prevElementIdx = indexMap.get(operation[0]);
9+
indexMap.put(operation[1], prevElementIdx);
10+
nums[prevElementIdx] = operation[1];
11+
}
12+
return nums;
13+
}
14+
}

0 commit comments

Comments
 (0)