Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill an new array with elements of array A that are missing form array B - Java

Tags:

java

arrays

So, basically i have two Arrays:

int[] listA = {2, -5, -121, 102, -35, -2, 0, -125, 802, -10};
int[] listB = {6, 99, -1, 12, 1, -2};

and I want to fill in a new array (listD) all elements of listA that are missing from listB.

The output should be like this:

Output: 2, -5, -121, 102, -35, 0, -125, 802, -10

My code is the following:

int arraySize = 0; //Variable to determine size of the new array;
int difElements = 0; //Variable to count every different element;

for(int i = 0; i < listA.length; i++){
    for(int j = 0; j < listB.length; j++){
        if(listA[i] != listB[j]){
            difElements++;
        }
        if(difElements == listB.length){
        arraySize++;
    }

    }
     difElements = 0;
}
System.out.println("Size of the array with different elements : " + arraySize);

int[] listD = new int [arraySize]; //Declaring array with specified size;

I then use the following for loop to run trough both arrays and check if there are repeated elements:

for (int i = 0; i < listA.length; i++) {
    for (int j = 0; j < listB.length; j++) {
        if (listA[i] != listB[j]) {
            listD[i] = listA[i];
        } else {
            listD[i] = listA[i + 1];
        }
    }
}

I know what's wrong with the loop, using Debugger it's quite easy to see where it goes wrong, but at this point i'm just running in circles and coding the same mistakes... After the loop finds the repeated element i can't find a way to normalize the condition (listD[i] = listA[i]) since it has skipped a postion. Maybe I need another loop just to iterate trough listD but I've never nested three loops before...

Bare in mind this is really basic java programming, these are just exercises from my class to get the basics of java programming. I have no knowledge of any POO, classes, methods etc etc and can't apply them of course.

like image 918
ramireeez Avatar asked Jan 24 '18 02:01

ramireeez


People also ask

How do you fill a 2D array in Java?

1)Fill 2D Array // given value. int [][]ar = new int [ 3 ][ 4 ]; // Fill each row with 10.


2 Answers

I suggest you to use one temporary array (say listC) to store the positions of the elements that we need to copy.

Iterating through both arrays for the first time, you calculate the size of the new array and set flags if the elements are unique.

Then you iterate through the listC and copy the unique elements from listA

int[] listA = {2, -5, -121, 102, -35, -2, 0, -125, 802, -10, 7, 555};
int[] listB = {6, 99, -1, 12, 1, -2, 7, 555};

// array of flags indicating which elements of listA to copy
int[] listC = new int[listA.length];

// counter of unique elements from listA
int counter = 0;

for (int i = 0; i < listA.length; i++) {
    boolean contains = false;
    //simplified form of for loop if you don't use an index
    for (int b: listB) {
        if (listA[i] == b) {
            contains = true;
            break;
        }
    }
    if (!contains) {
        counter++;
        //setting listC[i] to 1 if listA[i] is not present in listB
        listC[i] = 1;
    }
}

// declaring array with specified size 
int[] listD = new int[counter];

counter = 0;

// iterating through the array of flags to copy unique elements
for (int i = 0; i < listC.length; i++) {
    if (listC[i] == 1) {
        listD[counter++] = listA[i];
    }
}

System.out.println(Arrays.toString(listD));
like image 130
Kirill Simonov Avatar answered Sep 27 '22 19:09

Kirill Simonov


If you use java 8 you can use this.

List<Integer> aList = Arrays.stream(listA).boxed().collect(Collectors.toList());
List<Integer> bList = Arrays.stream(listB).boxed().collect(Collectors.toList());

aList.removeIf(o -> bList.contains(o));
like image 31
Chamly Idunil Avatar answered Sep 27 '22 19:09

Chamly Idunil