class Solution {
  public:
    int maxLength(vector<int>& arr) {
        int l = 0, r = 0, res = 0;
        set<int>has;
        while (r < arr.size()) {
            int tem = arr[r];
            if (has.count(tem)) {
                while (arr[l] != tem) {
                    has.erase(arr[l]);
                    l++;
                }
                l++;
            }
            has.insert(tem);
            res=max(res,r-l+1);
            r++;
        }
        return res;
    }
};