Advertisement
Guest User

function2

a guest
Aug 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class lab8function2 {
  4.  
  5.  
  6.     public static int[] merg(int[] arr1,int[] arr2){
  7.         int count =0 ;
  8.  
  9.         for(int i=0;i<arr1.length;i+=1){
  10.             for(int j=0;j<arr2.length;j+=1){
  11.                 if(arr1[i]==arr2[j]){
  12.                     count+=1;
  13.                 }
  14.             }
  15.         }
  16.         int k =0;
  17.         int[] arr3 = new int[count];
  18.         for(int i=0;i<arr1.length;i+=1){
  19.             for(int j=0;j<arr2.length;j+=1){
  20.                 if(arr1[i]==arr2[j]){
  21.                     arr3[k]=arr1[i];
  22.                     k+=1;
  23.                 }
  24.             }
  25.         }
  26.         return arr3;
  27.     }
  28.  
  29.     public static void main(String[] args) {
  30.         int[] arr1,arr2;
  31.         System.out.println("please enter first and second array size");
  32.         Scanner s = new Scanner(System.in);
  33.         arr1 = new int[s.nextInt()];
  34.         arr2 = new int[s.nextInt()];
  35.  
  36.         System.out.println("please enter values for first array");
  37.         for (int i=0;i<arr1.length;i+=1){
  38.             arr1[i] = s.nextInt();
  39.         }
  40.         System.out.println("please enter values for second array");
  41.         for (int i=0;i<arr2.length;i+=1){
  42.             arr2[i] = s.nextInt();
  43.         }
  44.  
  45.         int[] arr3 = merg(arr1,arr2);
  46.         for (int i=0;i<arr3.length;i+=1){
  47.             System.out.printf("%d ",arr3[i]);
  48.         }
  49.  
  50.         System.out.println();
  51.  
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement