int[][] arr1 = new int[][] {{1,2,1},{9,7,2},{7,3,6}};
int arr2[][] = new int[][] {{2,6,8},{0,1,7},{7,2,0},{8,3}};
boolean duplicate= false;
for(int i=0;i<arr1.length;i++) {
for(int j=0;j<arr1[i].length;j++) {
int transpose = 0;
transpose = arr1[i][j];
for(int k=0;k<arr2.length;k++) {
duplicate = false;
for(int l=0;l<arr2[k].length;l++) {
if(transpose == arr2[k][l]) {
System.out.println(transpose);
duplicate = true;
break;
}
}
if(duplicate)
break;
}
}
}
}
}
I tried to print common elements without using any shortcuts only logically with for loop and if statements. The logic is, in array 1 i am iterating each element and checking if it is there in array 2 if it is there we have to print it in console. But the problem is in array 1 element "1,2,7" is iterating 2 times and printing it 2 times.
getting this Output: 1 2 1 7 2 7 3 6
Expected Output: 1 2 7 3 6
Here, i have to stop iterating twice because it is already checked once printed. note: I want answer only with looping statements and conditional statements without hash set or shortcuts.
You'll need a loop for each array, and then a loop for each sub-array.
Additionally, I'm utilizing a Set here to prevent duplicate values.
Output
Ideally though, just add each value to a List, checking the List, first.