java solution using greedy approach with comments
answer is possible if a number appears == length of array.\n\npublic int minDominoRotationsOptimized(int[] tops, int[] bottoms) {\n\t\t//hold count of elements 
answer is possible if a number appears == length of array.\n```\npublic int minDominoRotationsOptimized(int[] tops, int[] bottoms) {\n\t\t//hold count of elements of top array\n\t\tint[] countA = new int[7];\n\t\t//hold count of elements of bottom array\n\t\tint[] countB = new int[7];\n\t\t//hold count of elements which appeared in both array.\n\t\tint[] same = new int[7];\n\t\t//length of array\n\t\tint n = tops.length;\n\t\t//iterate each element and prepare countA, countB and same frequencies\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tcountA[tops[i]]++;\n\t\t\tcountB[bottoms[i]]++;\n\t\t\tif (tops[i] == bottoms[i]) {\n\t\t\t\tsame[tops[i]]++;\n\t\t\t}\n\t\t}\n\t\t//iterate from i=1 till 6.\n\t\tfor (int i = 1; i < 7; i++) {\n\t\t\t//check for any value of i if total occurrence==n\n\t\t\tif (countA[i] + countB[i] - same[i] == n) {\n\t\t\t\t//if yes it means we have the answer which will be length minus maximum frequency of countA and countB.\n\t\t\t\treturn n - Math.max(countA[i], countB[i]);\n\t\t\t}\n\t\t}\n\t\t//answer not possible\n\t\treturn -1;\n\t}\n```
['Greedy', 'Java']