-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntersectionofTwoArrays.java
66 lines (56 loc) · 1.72 KB
/
IntersectionofTwoArrays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*Intersection of Two Arrays
You have been given two integer arrays/list(ARR1 and ARR2) of size N and M, respectively. You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists.*/
package Array_1D;
import java.util.Scanner;
public class IntersectionofTwoArrays
{
public static void Intersection(int arr1[], int[] arr2)
{
for(int i=0;i<arr1.length;i++)
{
for(int j=0;j<arr2.length;j++)
{
if(arr1[i]==arr2[j])
{
System.out.print(arr1[i]+" ");
arr2[j] = Integer.MIN_VALUE;
break;
}
}
}
}
public static void printArray(int arr[])
{
for(int i= 0; i<arr.length; i++)
{
System.out.print(arr[i]+ ", ");
}
System.out.println();
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array 1 : ");
int size1 = sc.nextInt();
int[] input_arr1 = new int[size1];
for(int i= 0; i<size1; i++)
{
System.out.print("Enter the element of array 1 "+ i + "th term : ");
input_arr1[i] = sc.nextInt();
}
System.out.println();
System.out.print("Enter the size of array 2 : ");
int size2 = sc.nextInt();
int[] input_arr2 = new int[size2];
for(int i= 0; i<size2; i++)
{
System.out.print("Enter the element of array 2 "+ i + "th term : ");
input_arr2[i] = sc.nextInt();
}
System.out.print("Array 1 : ");
printArray(input_arr1);
System.out.print("Array 2 : ");
printArray(input_arr2);
Intersection(input_arr1, input_arr2);
}
}