5 points static ArrayListSolutionMethod private static Array
Solution
Method:
private static ArrayList<Integer> Q3(ArrayList<Integer> input1,ArrayList<Integer> input2)
{
//Creating an ArrayList which stores the common elements
ArrayList<Integer> res=new ArrayList<Integer>();
Set s1=new HashSet<Integer>(input1);
Set s2=new HashSet<Integer>(input2);
s1.retainAll(s2);
for (Object temp : s1){
Integer ii=(int)temp;
res.add(ii);
}
return res;
}
______________________
Intersection.java
import java.util.*;
public class Intersection {
public static void main(String[] args) {
ArrayList<Integer> input1=new ArrayList<Integer>();
input1.add(2);
input1.add(5);
input1.add(7);
input1.add(8);
input1.add(11);
input1.add(11);
input1.add(14);
ArrayList<Integer> input2=new ArrayList<Integer>();
input2.add(1);
input2.add(5);
input2.add(12);
input2.add(6);
input2.add(11);
input2.add(11);
input2.add(10);
ArrayList<Integer> res=Q3(input1,input2);
for(Object o:res)
{
Integer i=(int)o;
System.out.println(i);
}
}
private static ArrayList<Integer> Q3(ArrayList<Integer> input1,
ArrayList<Integer> input2) {
//Creating an ArrayList which stores the common elements
ArrayList<Integer> res=new ArrayList<Integer>();
Set s1=new HashSet<Integer>(input1);
Set s2=new HashSet<Integer>(input2);
s1.retainAll(s2);
for (Object temp : s1){
Integer ii=(int)temp;
res.add(ii);
}
return res;
}
}
________________
output:
5
11
_______________

