class Solution {
public:
    int maxLength(vector<int>& arr) {
        int len = arr.size();
        if(len<=1)    return len;

        int maxLen = 1;
        set<int> S;
        int left = 0, right = 0;
        for(; right<len; right++) {
            while(S.find(arr[right]) != S.end()) {
                // 当集合 S中存在 arr[right]时,以数组 arr为依据,从left开始擦除,一直擦到S中没有arr[right]为止
                S.erase(arr[left++]);
            }
            S.insert(arr[right]);
            maxLen = max(maxLen, right-left+1);
        }

        return maxLen;
    }
};