Intersection of Two Arrays II LeetCode solution in Java

1
class Solution {
2
public int[] intersect(int[] nums1, int[] nums2) {
3
int[] dp = new int[1000 + 1];
4
for (int i : nums1) {
5
dp[i]++;
6
}
7
int ptr = 0;
8
int ans[] = new int[1000 + 1];
9
for (int i : nums2) {
10
if (dp[i] != 0) {
11
ans[ptr] = i;
12
ptr++;
13
dp[i]--;
14
}
15
}
16
return Arrays.copyOfRange(ans, 0, ptr);
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0