You can use a marker something of boolean
type to identify
- Element that is in listA and not in listB.
- Duplicate not found for adding to new list.
Now the code starts with three arrays
one listA
, listB
, listC
. listC
can only have size same as listA
, because that's the maximum (worst case that no elements of listA
match with listB
). Plus I'm adding my markers
int[] listA = {2, -5, -121, 102, -35, -2, 0, -125, 802, -10, 0, 2};
int[] listB = {6, 99, -1, 12, 1, -2};
int[] listC = new int[listA.length];
boolean elementFound = true;
boolean duplicateFound = false;
int thirdArrayIndex = 0;
int zeroCount = 0;
We will come to markers elementFound
and duplicateFound
later. First we will do the basic nested iteration
for(int i=0; i< listA.length; i++) {
boolean elementFound = true;
for(int j=0; j< listB.length; j++) {
Asking to iterate listB
with each element of listA
. Now inside the loop for listB
we keep a code for checking similarity of elements and setting the unique element mark.
if(listA[i] == listB[j]) {
elementFound = false;
break;
}
If element in listA
matches with listB
keep elementFound
as false and break the inner loop because no need to search again. That takes the execution to outer loop where we will check whether elementFound
. If it is true then only proceed to add the element.
The adding part will be like this
if(elementFound) {
boolean duplicateFound = false;
for(int k=0; k< listC.length; k++) {
if((listA[i] == listC[k]) || (listA[i] == 0 && ++zeroCount > 1)) {
duplicateFound = true;
break;
}
}
if(!duplicateFound) {
listC[thirdArrayIndex++] = listA[i];
}
}
Before the next for loop starts we will keep duplicateFound
as false
. Inside for loop we will check two conditions
- Whether element not in
listA
is already present in listC
.
- The initial
listC
will be having all elements initialized with 0
. So even if listA
is having element as 0
and which is not present in listB
, it won't be added to listC
because 0
is already present according to condition-1.
That's why the condition is like if((listA[i] == listC[k]) || (listA[i] == 0 && ++zeroCount > 1))
. Initially value of zeroCount
is 0
, so when a 0
is found in listA
not in listB
listA[i] == 0 --> true &&
++zeroCount(value = 1) >= 2 --> false
So the first 0
in listA
will be added. After the for loop with k
is over or either breaked
we will check not condition of duplicateFound
, ie no duplicate found in listC
, if that's true the go and add element of listA
to listC
listC[thirdArrayIndex++] = listA[i]; // listC[0] = listA[i]; first time
Only after the execution of this line the value of thirdArrayIndex
will be incremented refer How do the post increment (i++) and pre increment (++i) operators work in Java?.
Now when for loop with i
is finished all the elements in listA
not matching with listB
will be added to listC
, but if matching elements are found not all listA
elements will be added to listC
, so rest of the listC
elements will be having value 0
. That's why we will use a fourth array listD
to copy only the elements from listC
that were added from listA
.
That's where thirdArrayIndex
have its significance, by now thirdArrayIndex
means the total number of elements added from listA
to listC
. So the last part of the code will be like
int[] listD = new int[thirdArrayIndex];
for(int i=0; i<listD.length; i++) {
listD[i] = listC[i];
}
Finally print all the arrays
System.out.println(java.util.Arrays.toString(listA));
System.out.println(java.util.Arrays.toString(listB));
System.out.println(java.util.Arrays.toString(listC));
System.out.println(java.util.Arrays.toString(listD));
So final program will be like
int[] listA = {2, -5, -121, 102, -35, -2, 0, -125, 802, -10, 0, 2};
int[] listB = {6, 99, -1, 12, 1, -2};
int[] listC = new int[listA.length];
int thirdArrayIndex = 0;
int zeroCount = 0;
for(int i=0; i< listA.length; i++) {
boolean elementFound = true;
for(int j=0; j< listB.length; j++) {
if(listA[i] == listB[j]) {
elementFound = false;
break;
}
}
if(elementFound) {
boolean duplicateFound = false;
for(int k=0; k< listC.length; k++) {
if((listA[i] == listC[k]) || (listA[i] == 0 && ++zeroCount > 1)) {
duplicateFound = true;
break;
}
}
if(!duplicateFound) {
listC[thirdArrayIndex++] = listA[i];
}
}
}
int[] listD = new int[thirdArrayIndex];
for(int i=0; i<listD.length; i++) {
listD[i] = listC[i];
}
System.out.println(java.util.Arrays.toString(listA));
System.out.println(java.util.Arrays.toString(listB));
System.out.println(java.util.Arrays.toString(listC));
System.out.println(java.util.Arrays.toString(listD));