public int maxLength (int[] arr) { Deque<Integer> deque = new ArrayDeque<>(); int left = 0; int right = 0; int len = arr.length; int max = 0; while(right < len) { if(deque.contains(arr[right])) { // 包含则循环拿出前面的元素 while(deque.pollFirst()!=arr[right]){} } deque.offerLast(arr[right]); //添加元素 max = Math.max(max, deque.size()); right ++; } return max; }