利用哈希表定位重复元素,通过一次遍历求出结果
def main():
n = int(input())
s = list(map(int ,input().split(' ')))
a = {}
maxlens , lens , i = 0 , 0, 0
while i < n:
if s[i] not in a:
a[s[i]] = i #用字典保存,数值为key ,数值在列表中的序号为value
lens += 1 #字典中每增加一个元素,lens即不重复序列长度 加一
else : #出现重复数值
i = a[s[i]] #i值回到重复数值第一次出现的位置
a = {} #字典清空
maxlens = max(maxlens,lens) #maxlens保存最大长度
lens = 0
i += 1
maxlens = max(maxlens,lens) #与退出循环时lens比较,取最大值
print(maxlens)
main()