使用两个栈实现版本
import java.util.*;

public class Solution {
/**
*
* @param arr int整型一维数组 the array
* @return int整型
*/
public int maxLength (int[] arr) {
// write code here
Stack<integer> s1 = new Stack<>();
Stack<integer> s2 = new Stack<>();
int max = 0;
for (int i=0; i<arr.length; i++) {
if (!s1.isEmpty() && s1.contains(arr[i])) {
while (s1.peek() != arr[i]) {
s2.push(s1.pop());
}
s1.clear();
while (!s2.isEmpty()) {
s1.push(s2.pop());
}
}
s1.push(arr[i]);
max = Math.max(max, s1.size());
}
return max;</integer></integer>

}

}