fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner s=new Scanner(System.in);
  14. int n=s.nextInt();
  15. int a[]=new int[n];
  16.  
  17. for(int i=0;i<n;i++)
  18. a[i]=s.nextInt();
  19. int m=s.nextInt();
  20. int b[]=new int[m];
  21. for(int i=0;i<n;i++)
  22. b[i]=s.nextInt();
  23.  
  24. List<Integer> c=new ArrayList<>();
  25. for(int i=0;i<n;i++)
  26. {
  27. for(int j=0;j<m;j++)
  28. {
  29. if(a[i]==b[j])
  30. c.add(a[i]);
  31. }
  32. }
  33. System.out.print(c);
  34. }
  35.  
  36. }
Success #stdin #stdout 0.06s 2184192KB
stdin
5
1 2 3 4 5
5
5 6 7 8 9
stdout
[5]