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