Java Solution with idea description
my idea is \n\n- (intution) assuming there exists an answer - the repeating element is bound to be max in either of array (worst case half - half frequency in b
my idea is \n\n- (intution) assuming there exists an answer - the repeating element is bound to be max in either of array (worst case half - half frequency in both array, and half length is highest frequency)\n- do the following (for A,B and then B,A as swap can be in either direction* )\n- pick max freq element (**ele**) from the first array\n- check the occurence of **ele** in each index (if doesnt exist in either return -1 as incomplete index found)\n- if exists, in first array continue (no swap), in second array increment the swap value (as this is the index that has to be swapped)\n- in main function return appropriately (-1, or min of both options)\n\n```\nclass Solution {\n    public int minDominoRotations(int[] A, int[] B) {\n        int count1 = calculate(A, B);\n        int count2 = calculate(B, A);\n        if(count1 < 0 && count2 < 0) return -1;\n        if(count1 < 0) return count2;\n        if(count2 < 0) return count1;\n        return Math.min(count1, count2);\n    }\n    \n    public int calculate(int[] A, int[] B) {\n        Map<Integer, Integer> count = new HashMap<>();\n        int max = 0;\n        int ele = 0;\n        for(int a: A) {\n            int frequency = count.containsKey(a) ? count.get(a) + 1 : 1;\n            if(frequency > max) {\n                ele = a;\n                max = frequency;\n            }\n            count.put(a, frequency);\n        }\n        \n        int swaps = 0;\n        \n        for(int i = 0; i < B.length; i++) {\n            if(A[i] != ele && B[i] != ele) return -1;\n            if(A[i] == ele) continue;\n            if(B[i] == ele) swaps++;\n        }\n        \n        return swaps;\n    }\n}\n```\n\n*Return the minimum number of rotations so that all the values in A are the same, or all the values in B are the same.
[]