Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 - Write Java code to implement two different ways to find the intersection of

ID: 3577270 • Letter: 1

Question

1 - Write Java code to implement two different ways to find the intersection of two lists of integers, given in the form arrays A and B. The intersection of A and B will give you the integer values common to both A and B but not anything else. The two arrays are of different sizes. In one implementation you will only use arrays and no other data structures and in the other in addition to the arrays you will use a HashSet.   In both methods, 2ou will output the common values found in both A and B. You can test your code with some suitable values. (30 points).

Explanation / Answer

public class Main {
public static void main(String a[]){
int[] arr1 = {4,7,3,9,2};
int[] arr2 = {3,2,12,9,40,32,4};
int[] arr;
arr = intersection(arr1,arr2);
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]);
}
  
}
public static int[] intersection(int[] arr1, int[] arr2){
int[] arr;
int c=0;
for(int i=0;i<arr1.length;i++){
for(int j=0;j<arr2.length;j++){
if(arr1[i]==arr2[j]){
arr[c] = arr[i];
c=c+1;
}
}
}
return arr;
}
  
}