<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <code>n</code> positive integers.</p>

<p>The array <code>nums</code> is called <strong>alternating</strong> if:</p>

<ul>
	<li><code>nums[i - 2] == nums[i]</code>, where <code>2 &lt;= i &lt;= n - 1</code>.</li>
	<li><code>nums[i - 1] != nums[i]</code>, where <code>1 &lt;= i &lt;= n - 1</code>.</li>
</ul>

<p>In one <strong>operation</strong>, you can choose an index <code>i</code> and <strong>change</strong> <code>nums[i]</code> into <strong>any</strong> positive integer.</p>

<p>Return <em>the <strong>minimum number of operations</strong> required to make the array alternating</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,1,3,2,4,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
One way to make the array alternating is by converting it to [3,1,3,<u><strong>1</strong></u>,<u><strong>3</strong></u>,<u><strong>1</strong></u>].
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. 
</pre>

<p><strong class="example">Example 2:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

from collections import Counter

class Solution:
    def minimumOperations(self, nums: List[int]) -> int:
        # if array only has a single value, no operations needed
        if len(nums) == 1:
            return 0
        
        # count elements at even and odd indices
        evens = Counter(nums[::2])
        odds = Counter(nums[1::2])

        # get the most frequent element out of the odd-
        # and even-indexed elements
        max1 = max([(f,c) for c,f in evens.items()])
        max2 = max([(f,c) for c,f in odds.items()])

        # if the most frequent elements are not equal,
        # just return the number of elements in the array
        # that are not their corresponding most frequent element
        if max1[1] != max2[1]:
            return len(nums) - max1[0] - max2[0]
        # if the most frequent elements ARE equal, we need to change
        # one of the arrays to be comprised of its next most frequent
        # element
        else:
            # remove most frequent element so we can get second most
            # frequent
            evens.pop(max1[1])
            odds.pop(max1[1])

            # add a dummy value with 0 frequency in case
            # there is no second most frequent element
            evens['a'] = 0
            odds['a'] = 0

            # return the fewest number of operations between:
            # 1) take most-frequent of even-indexed and 2nd-most-frequent
            # of odd-indexed, or
            # 2) vice versa
            return len(nums) - max(
                max1[0] + max(odds.values()),
                max2[0] + max(evens.values())
            )
821