Advertisement
unknown_0711

Untitled

Oct 17th, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. public class Main
  7. {
  8.  
  9. public static void main (String[] args) throws java.lang.Exception
  10. {
  11. //your code here
  12. Scanner sc = new Scanner(System.in);
  13. int n = sc.nextInt();
  14. int[] first = new int[n];
  15. for (int i=0;i<n;i++)
  16. first[i] = sc.nextInt();
  17.  
  18. int m = sc.nextInt();
  19. int[] second = new int[m];
  20. for (int i=0;i<m;i++)
  21. second[i] = sc.nextInt();
  22.  
  23. ArrayList<Integer> ans = new ArrayList<>();
  24.  
  25. HashMap<Integer, Integer>hashMap = new HashMap<>();
  26. for(int i: second)
  27. hashMap.put(i, hashMap.getOrDefault(i, 0) - 1);
  28. for(int i: first)
  29. hashMap.put(i, hashMap.getOrDefault(i, 0) + 1 );
  30.  
  31. // for(Map.Entry<Integer, Integer> curr: hashMap.entrySet()) {
  32. // if(curr.getValue() != 0) {
  33. // ans.add(curr.getKey());
  34. // }
  35. // }
  36. for(int i: second){
  37. if(hashMap.get(i)!=0){
  38. ans.add(i);
  39. hashMap.put(i,0);
  40. }
  41. }
  42. for(int i: first){
  43. if(hashMap.get(i)!=0){
  44. ans.add(i);
  45. hashMap.put(i,0);
  46. }
  47. }
  48. Collections.sort(ans);
  49.  
  50. for (int x : ans)
  51. System.out.print(x + " ");
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement