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}
.
Thank you for your comments.
Asked
Active
Viewed 1,749 times
0

Amir Ebrahimi
- 547
- 1
- 7
- 22
-
what have you tried so far? – Nicolas Filotto Aug 27 '16 at 13:42
-
the answers [here](http://stackoverflow.com/questions/13732312/differences-between-two-arrays) may help you – guleryuz Aug 27 '16 at 13:42
-
post some code show us your effort – Khalil M Aug 27 '16 at 13:45
-
Convert to HashSet, do operation, convert back to array – OneCricketeer Aug 27 '16 at 13:47
-
@NicolasFilotto I am a rookie programmer and for this situation, I have to learn Algorithms which I'm not good at right now. have tried a code which crashes the program because it's looping through all objects of both arrays and gives a crash. the result is fine. i am working on this project for some time now and my time is getting over for that I've posted the question here. – Amir Ebrahimi Aug 27 '16 at 13:48
3 Answers
0
use this logic
int a1[n1],a2[n2],count1,count2,temp;
for(i=0;i<n1;i++)
{ temp=0;
for(j=0;j<n2;j++)
{
if(A[i]==B[j])
temp=1;
}
if(temp==0)
{a1[count1]=A[i];
count1++;
}
}
for(i=0;i<n2;i++)
{ temp=0;
for(j=0;j<n1;j++)
{
if(A[i]==B[j])
temp=1;
}
if(temp==0)
{a2[count2]=B[i];
count2++;
}
}
Now array a1[] contains the elements that are in A[] but not in B[] and a2[] contains the elements that are in B[] but not in A[].

Tanuj Yadav
- 1,259
- 13
- 21
0
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
{
// your code goes here
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)
{
System.out.println("Added element in B="+B[i]);
}
}
}
}

Abhishek
- 379
- 1
- 8
-
-
I have one more question can you describe what this code is doing? – Amir Ebrahimi Aug 27 '16 at 14:49
-
First you store all the element of second array in a map. and then traverse the first array (pick element one by one )and keep checking element is present in map or not. if element is present in map it means this is not missing element and if element does not present in map it means this is missing element of first array. – Abhishek Aug 27 '16 at 14:50
-
For second problem initially when I traverse the second array I put array element as key and value is one. it means in We have element and value 1 for each element of second array in map. now We traverse traverse first array again and if element present in map then We increase in value or put value 2 for element in map. this mean now element of first array which are present in map have value 2. Now traverse second array and check element in map . if this element having value less then 2 it means this element added in second array. – Abhishek Aug 27 '16 at 14:59
0
Use removeAll:
List<Integer> a = Arrays.asList(1 , 3 , 5 , 7 ,9);
List<Integer> b = Arrays.asList(1 ,3 , 4 ,5 , 6 ,7 , 10);
ArrayList<Integer> c = new ArrayList<>(a);
ArrayList<Integer> d = new ArrayList<>(b);
c.removeAll(b);
d.removeAll(a);
System.out.println(c);
System.out.println(d);

Alexey Soshin
- 16,718
- 2
- 31
- 40