这种板子也挺好用的,我在力扣上看到的,先入为主 (https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int res=0;
vector<int>arr;
for(int i=0;i<n;i++){
int x;
cin>>x;
arr.push_back(x);
//cout<<arr[i]<<" ";
}
map<int,bool>mp1;
for(int i=0,j=0;i<n;i++){
while(mp1[arr[i]]){
mp1[arr[j]]=0;
j++;
}
mp1[arr[i]]=1;
res=max(res,i-j+1);
}
cout<<res;
return 0;
}