import java.util.*;
public class Solution {
/**
*
* @param arr int整型一维数组 the array
* @return int整型
*/
public int maxLength (int[] arr) {
// write code here
int length = arr.length;
if(length==0 || length==1){
return length;
}
HashMap<Integer,Integer> hashMap = new HashMap<>();
int j = 0;//尾指针
int maxLen = 1;
for(int i=0;i<length;i++){//i为头指针
if(hashMap.containsKey(arr[i])){//当前数据已存在
j = Math.max(j,hashMap.get(arr[i])+1);//当存在重复元素时,更新尾指针
}
hashMap.put(arr[i],i);//压入,或者更新已存在的值
maxLen = Math.max(maxLen,i-j+1);//每次遍历都需要更新最大值
}
return maxLen;
}
}