desislava_topuzakova

02. Common Elements

Jun 5th, 2021
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class Demo_2 {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.        //1 ред -> масив1
  8.         //2 ред -> масив2
  9.  
  10.         String firstRow = scanner.nextLine(); //"Hey hello 2 4"
  11.         String secondRow = scanner.nextLine(); //"10 hey 4 hello"
  12.  
  13.         String[] array1 = firstRow.split(" "); //["Hey", "hello", "2", "4"]
  14.         String[] array2 = secondRow.split(" "); //["10", "hey", "4", "hello"]
  15.  
  16.         for (String secondWord : array2) {
  17.             for (String firstWord : array1) {
  18.                     if(secondWord.equals(firstWord)) {
  19.                         System.out.print(firstWord + " ");
  20.                     }
  21.             }
  22.         }
  23.  
  24.  
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment