• + 0 comments

    here is my solution

     unordered_map<int, pair<int,int>> NumberToIdx;
        int MinDist = INT_MAX;
    
        for(int i = 0 ; i < a.size(); ++i){
            
            if(NumberToIdx.find(a[i]) == NumberToIdx.end()){
                NumberToIdx[a[i]] = make_pair(i,INT_MAX);
            }
            else{
                if(NumberToIdx[a[i]].second == INT_MAX){
                    NumberToIdx[a[i]].second = i;
                    MinDist = min(MinDist , (NumberToIdx[a[i]].second - NumberToIdx[a[i]].first));
                }
    
                else{
                    int d = i - NumberToIdx[a[i]].second; 
                    MinDist = min(MinDist , d);
                }
            }
        }
    
        if(MinDist == INT_MAX)
            return -1;
        
        return MinDist;