+ 1
heelp java
static int[] analizaTablicy(int[] arr1, int[] arr2){ for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr2.length; j++) { if (arr1[i] != arr2[j]){ } } } } this is the fragment of code in which i need write function that will be return value of arr1 that doesn't containts in arr2. Please help me to finish the code
2 odpowiedzi
+ 1
Bram very nice explanation!
Kasia Herasymenko I strongly suggest to try to understand Bram's directions and make code based on that understanding. Also, I am providing you here slightly different solution, and that is just because fancy output :-D
https://code.sololearn.com/cA2VnX04hA86/?ref=app
+ 2
Before the 2 forloops you have to declare a new empty array for the difference.
In the 1st forloop:
int different = 1
In the 2nd forloop:
if (arr1[i] == arr2[j]){
different = 0
continue
}
After 2nd forloop, but still in the 1st forloop:
if (different) {
#append arr1[i] to new array
}
After the 2 forloops, return the array.
Now the function returns an array with all elements in arr1 that are not in arr2. The different variable is only set to 0 when a element in array2 is the same. So when it isnt 0, the current element of array1 is not in array2.