Intuitive solution java. count frequency , Greedy Apporach. ! Commented code.
Please upvote if helpful!!\n\n\npublic int minDominoRotations(int[] A, int[] B) {\n\n        Map<Integer, Integer> mapA = new HashMap<>();\n        Map<Integer,
**Please upvote if helpful!!**\n\n```\npublic int minDominoRotations(int[] A, int[] B) {\n\n        Map<Integer, Integer> mapA = new HashMap<>();\n        Map<Integer, Integer> mapB = new HashMap<>();\n\n        int maxA = -1; // the count of maximum repeated element in A.\n        int maxB = -1; // the count of maximum repeated element in B.\n        for (int i = 0; i < A.length; i++) {\n\n            if (mapA.containsKey(A[i])) {\n                final int countA = mapA.get(A[i]) + 1;\n                mapA.put(A[i], countA);\n                maxA = Math.max(countA, maxA);\n            } else {\n                mapA.put(A[i], 1);\n                maxA = Math.max(1, maxA);\n            }\n\n            if (mapB.containsKey(B[i])) {\n                final int countB = mapB.get(B[i]) + 1;\n                mapB.put(B[i], countB);\n                maxB = Math.max(countB, maxB);\n            } else {\n                mapB.put(B[i], 1);\n                maxB = Math.max(1, maxB);\n            }\n\n        }\n\n        boolean doesAHaveMaxRepeatedDominoNumber = maxA >= maxB;  // greedy approach , if maxA >= maxB we compare A with \n        // B else B with A.\n\n        int minRotations = 0;\n        if (doesAHaveMaxRepeatedDominoNumber) {\n            minRotations = getMinRotations(A, B, mapA, maxA);\n\n\n            //this check was added to check all the possibilities but it works without the below code!\n            //it checks B with A when A with B comparison returns -1 .\n//            if (minRotations == -1) {\n//                minRotations = getMinRotations(B, A, mapB, maxB);\n//            }\n\n        } else {\n            //compare B with A.\n            minRotations = getMinRotations(B, A, mapB, maxB);\n//            if (minRotations == -1) {\n//                minRotations = getMinRotations(A, B, mapA, maxA);\n//            }\n        }\n        return minRotations;\n    }\n\n    private int getMinRotations(int[] firstArray, int[] secondArray, Map<Integer, Integer> map, int max) {\n        int maxRepeatedValue = -1;\n\n        int minRotations = 0;\n        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {\n            //map key is element and value is its frequency.\n            //find the key of the most repeated element. \n            if (entry.getValue() == max) {\n                maxRepeatedValue = entry.getKey();\n                break;\n            }\n\n        }\n\n        //compare A and B array. to make the A have same element as the maxRepeated element. B should have \n        //element == maxRepeated element when A does not have element equal to maxRepeated Element.\n        //Else when A[i] != maxRepeatedValue and B[i] != maxRepeated value even with swapping B value ;\n        //All A elements will not have same value, so we break early. and return -1.\n        for (int i = 0; i < firstArray.length; i++) {\n            if (firstArray[i] != maxRepeatedValue && secondArray[i] == maxRepeatedValue) {\n                minRotations++;\n            } else if (firstArray[i] != maxRepeatedValue && secondArray[i] != maxRepeatedValue) {\n                minRotations = -1;\n                break;\n            }\n        }\n\n        return minRotations;\n    }\n```
['Greedy', 'Java']