 Want a dynamic java code to count number of time array2 present in array1 | SoloLearn: Learn to code for FREE!

0

# Want a dynamic java code to count number of time array2 present in array1

I have this static code to count, how many times array2 is present in array1, but I need a dynamic code i.e. even if I change array2 to {{10,20,30},{10,20,30}} it should give a proper count. This is a static code: public static void main(String[] args) { int array1[][]= {{10,20,30,40,50,60},{10,20,40,50,70,60},{10,20,30,40,50,60},{60,70,50,40,10,20},{80,90,70,60,10,20},{60,70,50,40,10,20}}; int array2[][]= {{10,20},{10,20}}; for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array1.length; j++) { System.out.print(array1[i][j]+"\t"); } System.out.println(); } int cnt=0; for (int i = 0; i < array1.length-1; i++) { for (int j = 0; j < array1.length-1; j++) { for (int k = 0; k < array2.length-1; k++) { for (int l = 0; l < array2.length-1; l++) { if((array1[i][j]==array2[k][l])&&(array1[i][j+1]==array2[k][l+1])&&(array1[i+1][j]==array2[k+1][l])&&(array1[i+1][j+1]==array2[k+1][l+1])) { cnt++; } } } } } System.out.println("\nCount for array2 present in array1 is: "+cnt++); }