Advertisement
desislava_topuzakova

02. Common Elements

Jun 5th, 2022
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Demo {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in); //sc
  10.  
  11.         String firstRow = scanner.nextLine(); //"Hey hello 2 4".split(" ") -> ["Hey", "hello", "2", "4"]
  12.         String secondRow = scanner.nextLine(); //"10 hey 4 hello".split(" ") -> ["10", "hey", "4", "hello"]
  13.  
  14.         String[] firstArray = firstRow.split(" ");
  15.         String[] secondArray = secondRow.split(" ");
  16.  
  17.         //обхождаме всеки елемент във втория масив
  18.         for (String secondElement : secondArray) {
  19.             //какво повтаряме за всеки елемент от втория масив: обхождаме първия масив
  20.             for (String firstElement : firstArray) {
  21.                 if (firstElement.equals(secondElement)) {
  22.                     System.out.print(firstElement + " ");
  23.                     break;
  24.                 }
  25.             }
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement