# LeetCode – First Duplicate Value

#### 问题：

[2,3,4,2,3,5] 返回2

[2,3,4,4,2,5] 返回4， 虽然2也是重复数字但是再2之后。

#### 解答：

```def firstDuplicateValue(array):
max = len(array)
value = -1
for i in range(len(array)):
for j in range(len(array)):
if (j > i and  array[j] == array[i]):
if (j < max):
max = j
value = array[i]
return value```

```def firstDuplicateValue(array):
seen = set()
for value in array:
if value in seen:
return value
return -1
```

```def firstDuplicateValue(array):
slow = fast = 0
while True:
if slow > len(array)-1 or fast>len(array)-1:
return -1
if array[fast] > len(array)-1:
return -1
slow = array[slow]
fast = array[array[fast]]
if slow == fast:
break
slow = 0
while slow!=fast:
slow = array[slow]
fast = array[fast]
return slow```

```def firstDuplicateValue(array):
for value in array:
absvalue = abs(value)
if array[absvalue - 1] < 0:
return absvalue
array[absvalue-1] *= -1;
return -1
```

#### 参考及引用

https://www.algoexpert.io/questions/first-duplicate-value

https://leetcode.cn/problems/find-the-duplicate-number/solutions/2361404/287-xun-zhao-zhong-fu-shu-yuan-di-jiao-h-0eja/?utm_source=LCUS&utm_medium=ip_redirect&utm_campaign=transfer2china