Amir Ebrahimi - 11 months ago 65
Java Question

Intersection between two arrays

I have a problem finding the difference between two arrays in java, my case is like imagine we have two arrays. array

A = {1 , 3 , 5 , 7 ,9 }
and array
B = {1 ,3 , 4 ,5 , 6 ,7 , 10}
. I want to have two results first result is an array which finds the missing objects from array "A" , and the second result is an array which finds the added objects in array "B". the first result should be Like
A'={9}
and the second result is like
B'={4,6,10}
.

I think following code helps you

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{

Map<Integer,Integer> map1=new HashMap<Integer,Integer>();
int A[]={1 , 3 , 5 , 7 ,9 };
int B[]={1 ,3 , 4 ,5 , 6 ,7 , 10};
int i;
for(i=0;i<B.length;i++)
map1.put(B[i],1);
for(i=0;i<A.length;i++)
{

Integer v1=map1.get(A[i]);
if(v1==null)
{

System.out.println("Missing number="+A[i]);
}

}

for(i=0;i<A.length;i++)
{

Integer v1=map1.get(A[i]);

if(v1!=null)
{int val=v1;
map1.put(A[i],val+1);

//      System.out.println("Missing number="+A[i]);
}

}
for(i=0;i<B.length;i++)
{
Integer v1=map1.get(B[i]);
if(v1!=null && v1<2)
{