import java.util.Arrays;
/**
* Intersection and Union of Arrays
* @author Prateek
*/
class IntersectionAndUnion {
private int[] arr1;
private int[] arr2;
public IntersectionAndUnion(int[] arr1, int[] arr2) {
this.arr1 =arr1;
this.arr2 =arr2;
}
public static void main
(String[] args
) { int[] arr1={7, 5, 4, 3, 1};
int[] arr2= {2, 3, 5, 6};
IntersectionAndUnion obj=new IntersectionAndUnion(arr1 , arr2);
System.
out.
println("Union List: "); obj.union();
System.
out.
println("Intersection List: "); obj.intersection();
}
/**
* Union Of arrays
*/
public void union() {
int i=0,j=0;
while(i < arr1.length && j < arr2.length)
{
if(arr1[i] < arr2[j])
else if(arr1[i] > arr2[j])
else{
j++;
}
}
if(arr2.length > arr1.length)
for(;j
<arr2.
length;System.
out.
println(arr2
[j
]),j
++); else if(arr2.length < arr1.length)
for( ; i
<arr1.
length;System.
out.
println(arr1
[i
]),i
++); }
/**
* Intersection of Arrays
*/
public void intersection() {
int i=0;
int j=0;
while(i < arr1.length && j < arr2.length)
{
if(arr1[i] < arr2[j])
i++;
else if(arr1[i] > arr2[j])
j++;
else
{
System.
out.
print(arr1
[i
++] + "\t"); j++;
}
}
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7Ci8qKgogKiBJbnRlcnNlY3Rpb24gYW5kIFVuaW9uIG9mIEFycmF5cwogKiBAYXV0aG9yIFByYXRlZWsKICovCmNsYXNzIEludGVyc2VjdGlvbkFuZFVuaW9uIHsKCglwcml2YXRlIGludFtdIGFycjE7Cglwcml2YXRlIGludFtdIGFycjI7CgoJcHVibGljIEludGVyc2VjdGlvbkFuZFVuaW9uKGludFtdIGFycjEsIGludFtdIGFycjIpIHsKCQl0aGlzLmFycjEgPWFycjE7CgkJdGhpcy5hcnIyID1hcnIyOwoJCUFycmF5cy5zb3J0KGFycjEpOwoJCUFycmF5cy5zb3J0KGFycjIpOwoJfQoKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKCQlpbnRb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