You are given an integer array cards where cards[i] represents the value of the ith card. A pair of cards are matching if the cards have the same value.

Return the minimum number of consecutive cards you have to pick up to have a pair of matching cards among the picked cards. If it is impossible to have matching cards, return -1.

&nbsp;
Example 1:

Input: cards = [3,4,2,3,4,7]
Output: 4
Explanation: We can pick up the cards [3,4,2,3] which contain a matching pair of cards with value 3. Note that picking up the cards [4,2,3,4] is also optimal.


Example 2:

Input: cards = [1,0,5,3]
Output: -1
Explanation: There is no way to pick up a set of consecutive cards that contain a pair of matching cards.


&nbsp;
Constraints:


	1 &lt;= cards.length &lt;= 105
	0 &lt;= cards[i] &lt;= 106


class Solution:
    def minimumCardPickup(self, cards: List[int]) -> int:
        d={}
        x=[]
        for i in range(len(cards)):
            if cards[i] not in d:
                d[cards[i]]=i
            else:
                x.append(i-d[cards[i]])
                d[cards[i]]=i
        if len(x)<=0:
            return -1
        return min(x)+1
class Solution
{
    public int minimumCardPickup(int[] cards)
    {
        Map<Integer,Integer> map = new HashMap<>();
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < cards.length; i++)
        {
            if(map.containsKey(cards[i]))
                min = Math.min(i-map.get(cards[i])+1,min); // Check if the difference in indices is smaller than minimum
            map.put(cards[i],i); // Update the last found index of the card
        }
        return min == Integer.MAX_VALUE?-1:min; // Repetition found or not
    }
}
class Solution {
public:
    int minimumCardPickup(vector<int>& cards) {

        int res (INT_MAX), n(size(cards));
        unordered_map<int, int> m;
        for (auto i=0; i<n; i++) {
            // number of consecutive cards you have to pick up to have a pair of matching cards == (Diference between 2 indexes of same card) + 1
            if (m.count(cards[i])) res = min(res, i-m[cards[i]]+1);
            m[cards[i]] = i;
        }
        return (res == INT_MAX) ? -1 : res;
    }
};
var minimumCardPickup = function(cards) {
    let cardsSeen = {};
    let minPicks = Infinity;
    for (let i = 0; i < cards.length; i++) {
        if (!(cards[i] in cardsSeen)) {
            cardsSeen[cards[i]] = i;
        } else {
            const temp = i - cardsSeen[cards[i]] + 1;
            minPicks = Math.min(minPicks, temp);
            cardsSeen[cards[i]] = i;
        }
    }
    return minPicks === Infinity ? -1 : minPicks;
};
Minimum Consecutive Cards to Pick Up