Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int[] intersect(int[] nums1, int[] nums2) {
- HashMap<Integer, Integer> map = new HashMap<>();
- for(final int n : nums1) {
- map.put(n, map.getOrDefault(n, 0) + 1);
- }
- ArrayList<Integer> tmpAns = new ArrayList<>();
- for(final int n : nums2) {
- if(map.containsKey(n) && map.put(n, map.get(n)-1) > 0) {
- tmpAns.add(n);
- }
- }
- int[] ans = new int[tmpAns.size()];
- int i = 0;
- for(final int n : tmpAns) {
- ans[i++] = n;
- }
- return ans;
- }
- }
Add Comment
Please, Sign In to add comment