TheBulgarianWolf

Equal Elements In 2 Arrays

Apr 27th, 2020
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. public class SoftUni {  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         System.out.print("Enter the first line of elements: ");
  8.         String first = sc.nextLine();
  9.         String[] firstArray = first.split(" ");
  10.         System.out.print("Enter the second line of elements: ");
  11.         String second = sc.nextLine();
  12.         String[] secondArray = second.split(" ");
  13.        
  14.         //setting for-loop limits
  15.         int arraySize;
  16.         int arraySize2;
  17.         if(firstArray.length > secondArray.length){
  18.             arraySize = firstArray.length;
  19.             arraySize2 = secondArray.length;
  20.         }
  21.         else{
  22.             arraySize = secondArray.length;
  23.             arraySize2 = firstArray.length;
  24.         }
  25.         String endString = "";
  26.         for(int i = 0;i<arraySize;i++){
  27.             for(int j = 0;j<arraySize2;j++){
  28.                 if(firstArray[i].equals(secondArray[j])){
  29.                     endString += firstArray[i] + " ";
  30.                 }
  31.             }
  32.         }
  33.         System.out.println(endString);
  34.  
  35.     }
  36. }
Add Comment
Please, Sign In to add comment