In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)

We may rotate the ith domino, so that tops[i] and bottoms[i] swap values.

Return the minimum number of rotations so that all the values in tops are the same, or all the values in bottoms are the same.

If it cannot be done, return -1.

&nbsp;
Example 1:

Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]
Output: 2
Explanation: 
The first figure represents the dominoes as given by tops and bottoms: before we do any rotations.
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.


Example 2:

Input: tops = [3,5,1,2,3], bottoms = [3,6,3,3,4]
Output: -1
Explanation: 
In this case, it is not possible to rotate the dominoes to make one row of values equal.


&nbsp;
Constraints:


	2 &lt;= tops.length &lt;= 2 * 104
	bottoms.length == tops.length
	1 &lt;= tops[i], bottoms[i] &lt;= 6


class Solution:
    def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
        sames = [tops[i] for i in range(len(tops)) if tops[i] == bottoms[i]]
		
        same_count = collections.Counter(sames)
        bottom_count = collections.Counter(bottoms)
        top_count = collections.Counter(tops)
        
        for n in range(1,7):
            if bottom_count[n] + top_count[n] - same_count[n] == len(tops):
                return min(bottom_count[n], top_count[n]) - same_count[n]
            
        return -1
class Solution {
    public int minDominoRotations(int[] tops, int[] bottoms) {

        int[][] c = new int[6][2];

        for (int i : tops) {
            c[i - 1][0]++;
        }
        for (int i : bottoms) {
            c[i - 1][1]++;
        }
        int[] common = new int[6];
        for (int i = 0; i < tops.length; i++) {
            if (tops[i] == bottoms[i]) {
                common[tops[i] - 1]++;
            }
        }
        int min = Integer.MAX_VALUE;
        for (int i = 1; i <= 6; i++) {
            if (c[i - 1][0] + c[i - 1][1] >= tops.length) {
                if (c[i - 1][0] >= c[i - 1][1] && c[i - 1][1] - common[i - 1] + c[i - 1][0] == tops.length) {
                    min = Math.min(min, c[i - 1][1] - common[i - 1]);
                }
                else if (c[i - 1][1] >= c[i - 1][0] && c[i - 1][0] - common[i - 1] + c[i - 1][1] == tops.length) {
                    int left = c[i - 1][0] - common[i - 1];
                    min = Math.min(min, c[i - 1][0] - common[i - 1]);
                }
            }
        }

        return min == Integer.MAX_VALUE ? -1 : min;
    }
}
class Solution {
public:
    int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
        vector<int> freq(7, 0);
        
        int n = tops.size();
        int tile = -1
		// there has to be particular tile number which is present in every column to be able to arrange same tile in top or bottom by rotating
        // check which tile is present in every column
        for(int i=0; i<n; ++i){
            int top = tops[i];
            int bottom = bottoms[i];
            
            freq[top]++;
            if(top != bottom){
                freq[bottom]++;
            }
            //  check for potential tile number
            if(freq[top] == n){
                tile = top;
            }
            if(freq[bottom] == n){
                tile = bottom;
            }
        }
        
        if(tile == -1){ // rearrangement not possible
            return -1;
        }
        
        int tilesTop = 0;
        int tilesBottom = 0;
        for(int i=0; i<n; ++i){
            if(tops[i] == bottoms[i])continue;
            
            if(tops[i] == tile){
                tilesTop++;
            }
            if(bottoms[i] == tile){
                tilesBottom++;
            }
        }
        
      return tilesTop < tilesBottom ? tilesTop : tilesBottom;
    }
};
/**
 * @param {number[]} tops
 * @param {number[]} bottoms
 * @return {number}
 */
var minDominoRotations = function(tops, bottoms) {
    const swaps = Math.min(
        minimum(tops[0], tops, bottoms),
        minimum(tops[0], bottoms, tops),
        minimum(bottoms[0], tops, bottoms),
        minimum(bottoms[0], bottoms, tops)
    );

    return swaps === Infinity ? -1 : swaps;

    function minimum(target, x, y, count = 0) {
        for(let i = 0; i < x.length; i++) {
            if(target !== x[i]) {
                if (target !== y[i]) return Infinity
                count++;
            }
        }
        return count;
    }
};
Minimum Domino Rotations For Equal Row