You are given a 0-indexed array nums consisting of n positive integers.

The array nums is called alternating if:


	nums[i - 2] == nums[i], where 2 &lt;= i &lt;= n - 1.
	nums[i - 1] != nums[i], where 1 &lt;= i &lt;= n - 1.


In one operation, you can choose an index i and change nums[i] into any positive integer.

Return the minimum number of operations required to make the array alternating.

&nbsp;
Example 1:

Input: nums = [3,1,3,2,4,3]
Output: 3
Explanation:
One way to make the array alternating is by converting it to [3,1,3,1,3,1].
The number of operations required in this case is 3.
It can be proven that it is not possible to make the array alternating in less than 3 operations. 


Example 2:

Input: nums = [1,2,2,2,2]
Output: 2
Explanation:
One way to make the array alternating is by converting it to [1,2,1,2,1].
The number of operations required in this case is 2.
Note that the array cannot be converted to [2,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= 105


class Solution:
    def minimumOperations(self, nums: List[int]) -> int:
        n = len(nums)
        odd, even = defaultdict(int), defaultdict(int)
        for i in range(n):
            if i % 2 == 0:
                even[nums[i]] += 1
            else:
                odd[nums[i]] += 1
        topEven, secondEven = (None, 0), (None, 0)
        for num in even:
            if even[num] > topEven[1]:
                topEven, secondEven = (num, even[num]), topEven
            elif even[num] > secondEven[1]:
                secondEven = (num, even[num])
        topOdd, secondOdd = (None, 0), (None, 0)
        for num in odd:
            if odd[num] > topOdd[1]:
                topOdd, secondOdd = (num, odd[num]), topOdd
            elif odd[num] > secondOdd[1]:
                secondOdd = (num, odd[num])
        if topOdd[0] != topEven[0]:
            return n - topOdd[1] - topEven[1]
        else:
            return n - max(secondOdd[1] + topEven[1], secondEven[1] + topOdd[1])
class Solution {
    public int minimumOperations(int[] nums) {
        int freq[][] = new int[100005][2];
        int i, j, k, ans=0;
        for(i = 0; i < nums.length; i++) {
    			freq[nums[i]][i&1]++;
    		}
    		
    		for(i = 1, j=k=0; i <= 100000; i++) {
			// Add the maximum frequency of odd indexes to maximum frequency even indexes 
		    //and vice versa, it will give us how many elements we don't need to change. 
    		ans = Math.max(ans, Math.max(freq[i][0] + k, freq[i][1] + j));
            j = Math.max(j, freq[i][0]);
            k = Math.max(k, freq[i][1]);
        }
        return nums.length - ans;
    }
}
class Solution {
public:
    int minimumOperations(vector<int>& nums) {

        int totalEven = 0, totalOdd = 0;

        unordered_map<int,int> mapEven, mapOdd;

        for(int i=0;i<nums.size();i++) {
            if(i%2==0) {
                totalEven++;
                mapEven[nums[i]]++;
            }

            else {
                totalOdd++;
                mapOdd[nums[i]]++;
            }
        }

        int firstEvenCount = 0, firstEven = 0;
        int secondEvenCount = 0, secondEven = 0;

        for(auto it=mapEven.begin();it!=mapEven.end();it++) {
            int num = it->first;
            int count = it->second;

            if(count>=firstEvenCount) {
                secondEvenCount = firstEvenCount;
                secondEven = firstEven;
                firstEvenCount = count;
                firstEven = num;
            }

            else if(count >= secondEvenCount) {
                secondEvenCount = count;
                secondEven = num;
            }
        }

        int firstOddCount = 0, firstOdd = 0;
        int secondOddCount = 0, secondOdd = 0;

        for(auto it=mapOdd.begin();it!=mapOdd.end();it++) {
            int num = it->first;
            int count = it->second;

            if(count>=firstOddCount) {
                secondOddCount = firstOddCount;
                secondOdd = firstOdd;
                firstOddCount = count;
                firstOdd = num;
            }

            else if(count>=secondOddCount) {
                secondOddCount = count;
                secondOdd = num;
            }
        }

        int operationsEven = 0, operationsOdd = 0;

        operationsEven = totalEven - firstEvenCount;

        if(firstEven!=firstOdd) operationsEven += (totalOdd - firstOddCount);
        else operationsEven += (totalOdd - secondOddCount);

        operationsOdd = totalOdd - firstOddCount;
        if(firstOdd!=firstEven) operationsOdd += (totalEven - firstEvenCount);
        else operationsOdd += (totalEven - secondEvenCount);

        return min(operationsEven, operationsOdd);

    }
};
/**
 * @param {number[]} nums
 * @return {number}
 */

var minimumOperations = function(nums) {
    let countOddId = {}    
    let countEvenId = {}
    if(nums.length === 1) return 0
    if(nums.length === 2 && nums[0] === nums[1]) {
        return 1
    }
    nums.forEach((n, i) => {
        if(i%2) {
            if(!countOddId[n]) {
                countOddId[n] = 1;
            } else {
                countOddId[n]++
            }
        } else {
            if(!countEvenId[n]) {
                countEvenId[n] = 1;
            } else {
                countEvenId[n]++
            }
        }
    })
    
    const sortedEven = Object.entries(countEvenId).sort((a, b) => {
        return  b[1] - a[1]
    })

    const sortedOdd = Object.entries(countOddId).sort((a, b) => {
        return  b[1] - a[1]
    })

    if(sortedEven[0][0] === sortedOdd[0][0]) {
        let maxFirst =0;
        let maxSec =0;

        if(sortedEven.length === 1) {
            maxFirst = sortedEven[0][1];
            maxSec = sortedOdd[1]? sortedOdd[1][1] : 0;
            return  nums.length - (maxFirst + maxSec)  
        } 
        
        if(sortedOdd.length === 1) {
            maxFirst = sortedOdd[0][1];
            maxSec = sortedEven[1] ? sortedEven[1][1] : 0;
            return  nums.length - (maxFirst + maxSec)  
        }
        if(sortedEven[0][1] >= sortedOdd[0][1] && sortedEven[1][1] <= sortedOdd[1][1]) {
            maxFirst = sortedEven[0][1]
            maxSec = sortedOdd[1][1]
            return  nums.length - (maxFirst + maxSec)  
        } else {
            maxFirst = sortedOdd[0][1]
            maxSec = sortedEven[1][1]
            return  nums.length - (maxFirst + maxSec)  
        }
    } else {
        return nums.length - (sortedEven[0][1] + sortedOdd[0][1])
    }

};
Minimum Operations to Make the Array Alternating