- codewithshiva

Latest

Search Bar

Tuesday, July 6, 2021

 Intersection of Array

Description

You are given 2 arrays of N integers. Your task is to write a program that finds the one integer which is common in both arrays.

Note: There is always one integer common in both arrays.

Input

Input Format

First line of input contains N

Second line contains N space separated integers making the first array

Third line contains N space separated integers making the second array


Constraints

N<1000

Output

Output Format

Output that one integer which is common in both arrays

Sample Input 1 

3
4 5 7
9 2 5

Sample Output 1

5

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int inps= sc.nextInt();
        int []array1 = new int[inps];
        int []array2 = new int[inps];

        for (int i = 0 ; i <inps;i++){
            array1[i]=sc.nextInt();
        }
        for (int i = 0 ; i <inps;i++){
            array2[i]=sc.nextInt();
        }

        for (int i = 0 ; i <inps;i++){
            for (int j = 0 ; j <inps;j++){
                if (array1[i]==array2[j]){
                    System.out.println(array1[i]);
                    break;
                }
            }
        }
    }


No comments:

Post a Comment