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[0].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[0].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++); }

3/28/2017 6:46:27 AM

Purshottam Kale

3 Answers

New Answer

+13

This should do: https://code.sololearn.com/cGBdMNf5dTqJ/?ref=app

0

No!!! I want a count of whole 2D array in another 2D array.

0

Please run the code which I have given above. You will understand it properly. The code which I have given above is static but I want it dynamic i.e. even if I change array2[][]={{20,30},{20,40}} the output must be 1 as it is present for 1 time in array1.