Didart

Common Elements

Oct 3rd, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class CommonElements {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String firstLine = scanner.nextLine();
  8.         String secondLine = scanner.nextLine();
  9.  
  10.         String[] array1 = firstLine.split(" ");
  11.         String[] array2 = secondLine.split(" ");
  12.  
  13.         for (String elementInSecond : array2) {
  14.             for (String elementInFirst : array1) {
  15.                 if (elementInSecond.equals(elementInFirst)) {
  16.                     System.out.print(elementInSecond + " ");
  17.                 }
  18.             }
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment