Guest User

Untitled

a guest
Dec 17th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. class Solution {
  2. public int[] intersect(int[] nums1, int[] nums2) {
  3. HashMap<Integer, Integer> map = new HashMap<>();
  4.  
  5. for(final int n : nums1) {
  6. map.put(n, map.getOrDefault(n, 0) + 1);
  7. }
  8.  
  9. ArrayList<Integer> tmpAns = new ArrayList<>();
  10. for(final int n : nums2) {
  11. if(map.containsKey(n) && map.put(n, map.get(n)-1) > 0) {
  12. tmpAns.add(n);
  13. }
  14. }
  15.  
  16. int[] ans = new int[tmpAns.size()];
  17. int i = 0;
  18. for(final int n : tmpAns) {
  19. ans[i++] = n;
  20. }
  21.  
  22. return ans;
  23. }
  24. }
Add Comment
Please, Sign In to add comment