[Question]
  [
Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index. In other words, if there are more than 1 duplicated numbers, return the number for which the second occurrence has a smaller index than the second occurrence of the other number does. If there are no such elements, your program / function may result in undefined behaviour.
Example:
For `a = [2, 3, 3, 1, 5, 2]`, the output should be
`firstDuplicate(a) = 3`.
There are 2 duplicates: numbers 2 and 3. The second occurrence of 3 has a smaller index than the second occurrence of 2 does, so the answer is 3.
For `a = [2, 4, 3, 5, 1]`, the output should be
`firstDuplicate(a) = -1`.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest answer in bytes wins.
BONUS: Can you solve it in O(n) time complexity and O(1) additional space complexity?
  
[Answer]
# [Python 2](https://docs.python.org/2/), 34 bytes
*O(n2) time, O(n) space*
Saved 3 bytes thanks to @vaultah, and 3 more from @xnor!
```
lambda l:l[map(l.remove,set(l))<0]
```
[Try it online!](https://tio.run/##TYqxDoJAEERr7iu2hOQ0gtpcpMTEysYOKQ5ZAnE5Luuh8PUnkpA4mWbePDu5pjeJr9O7J92VlQZSlHfahrRl7Po3yhe6kKLotCv8p2kJ4cYDKgFzHE9KBJS2xg4ujERguTUO6tlffhwfaB1cTIVjxtyzWpVN/C9k1/N6l4z66fNEwn5pLOEoISnEDx0WNO@4@AI "Python 2 – Try It Online")
[Answer]
## JavaScript (ES6), ~~47~~ ~~36~~ ~~31~~ 25 bytes
*Saved 6 bytes thanks to ThePirateBay*
Returns `undefined` if no solution exists. 
Time complexity: *O*(n) :-)  
Space complexity: *O*(n) :-( 
```
a=>a.find(c=>!(a[-c]^=1))
```
### How?
We keep track of already encountered values by saving them as new *properties* of the original array ***a*** by using negative numbers. This way, they can't possibly interfere with the original entries.
### Demo
```
let f =
a=>a.find(c=>!(a[-c]^=1))
console.log(f([2, 3, 3, 1, 5, 2]))
console.log(f([2, 4, 3, 5, 1]))
console.log(f([1, 2, 3, 4, 1]))
```
[Answer]
# Mathematica, 24 bytes
```
#/.{h=___,a_,h,a_,h}:>a&
```
Mathematica's pattern matching capability is so cool!
Returns the original `List` for invalid input.
### Explanation
```
#/.
```
In the input, replace...
```
{h=___,a_,h,a_,h}
```
A `List` with a duplicate element, with 0 or more elements before, between, and after the duplicates...
```
... :>a
```
With the duplicate element.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
Ṛœ-QṪ
```
[Try it online!](https://tio.run/##y0rNyan8///hzllHJ@sGPty56v///9FGOgrGYGSoo2Cqo2AUCwA "Jelly – Try It Online")
### How it works
```
Ṛœ-QṪ  Main link. Argument: A (array)
Ṛ      Yield A, reversed.
   Q   Unique; yield A, deduplicated.
 œ-    Perform multiset subtraction.
       This removes the rightmost occurrence of each unique element from reversed
       A, which corresponds to the leftmost occurrence in A.
    Ṫ  Take; take the rightmost remaining element, i.e., the first duplicate of A.
```
[Answer]
# Pyth, 5 bytes
```
h.-Q{
```
[Test suite](https://pyth.herokuapp.com/?code=h.-Q%7B&test_suite=1&test_suite_input=%5B2%2C+3%2C+3%2C+1%2C+5%2C+2%5D%0A%5B2%2C+4%2C+3%2C+5%2C+1%5D%0A%5B1%2C+2%2C+1%2C+2%5D&debug=0)
Remove from Q the first appearance of every element in Q, then return the first element.
[Answer]
# [Haskell](https://www.haskell.org/), 35 bytes
```
f s(h:t)|h`elem`s=h|1<2=f(h:s)t
f[]
```
[Try it online!](https://tio.run/##HchBCsMgEAXQvaf4hCwaMIFYuin1AD2DSJPF2JEaExyXubsthbd6vMqHUmotQC58r8PJCyXaFrF8zg9jw29lqCo43/CyVMpeum4c8XznvRAqR0GKmSal1LbGDIujxFzRI8B5OKNx/Zs1bhrGty8 "Haskell – Try It Online") Crashes if no duplicate is found.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 4 bytes
```
UÞ⊍h
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJQIiwiIiwiVcOe4oqNaCIsIiIsIlsyLCAxLCA0LCAxLCA0XSJd)
Takes the multi-set symmetric difference, which outputs the duplicate values in order that they occur. Outputs `0` if nothing is found.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 bytes
```
xŒQ¬$Ḣ
```
[Try it online!](https://tio.run/##y0rNyan8/7/i6KTAQ2tUHu5Y9P9w@6OmNf//R0cb6SgYg5GhjoKpjoJRrA6XAkjQBCwIFDEEiwBlISpNYCKmYK4RWI1JbCwA)
Returns the first duplicate, or 0 if there is no duplicate.
## Explanation
```
xŒQ¬$Ḣ  Input: array M
    $   Operate on M
 ŒQ       Distinct sieve - Returns a boolean mask where an index is truthy
          for the first occurrence of an element
   ¬      Logical NOT
x       Copy each value in M that many times
     Ḣ  Head
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 7 bytes
```
æ@bX ¦Y
```
[Test it online!](http://ethproductions.github.io/japt/?v=1.4.5&code=5kBiWCCmWQ==&input=WzIsIDMsIDMsIDEsIDUsIDJd)
### Explanation
```
 æ@   bX ¦ Y
UæXY{UbX !=Y}  Ungolfed
               Implicit: U = input array
UæXY{       }  Return the first item X (at index Y) in U where
     UbX         the first index of X in U
         !=Y     is not equal to Y.
               In other words, find the first item which has already occured.
               Implicit: output result of last expression
```
---
Alternatively: 
```
æ@¯Y øX
```
[Test it online!](http://ethproductions.github.io/japt/?v=1.4.5&code=5kCvWSD4WA==&input=WzIsIDMsIDMsIDEsIDUsIDJd)
### Explanation
```
 æ@   ¯ Y øX
UæXY{Us0Y øX}  Ungolfed
               Implicit: U = input array
UæXY{       }  Return the first item X (at index Y) in U where
     Us0Y        the first Y items of U (literally U.slice(0, Y))
          øX     contains X.
               In other words, find the first item which has already occured.
               Implicit: output result of last expression
```
[Answer]
# Dyalog APL, ~~27~~ ~~24~~ ~~20~~ ~~19~~ ~~13~~ ~~12~~ 11 bytes
```
⊢⊃⍨0⍳⍨⊢=⍴↑∪
```
Now modified to not depend on v16! [Try it online!](http://tryapl.org/?a=%28%u22A2%u2283%u23680%u2373%u2368%u22A2%3D%u2374%u2191%u222A%29%201%201&run)
# How? (With input **N**)
* `⊢⊃⍨...` - **N** at this index:
	+ `⍴↑∪` - **N** with duplicates removed, right-padded with `0` to fit **N**
	+ `⊢=` - Element-wise equality with **N**
	+ `0⍳⍨` - Index of the first `0`. `
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 5 bytes
```
a⊇=bh
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/P/FRV7ttUsb//9FGOgrGYGSoo2Cqo2AU@z8KAA "Brachylog – Try It Online")
## Explanation
```
a⊇=bh  Input is a list.
a      There is an adfix (prefix or suffix) of the input
 ⊇     and a subsequence of that adfix
  =    whose elements are all equal.
   b   Drop its first element
    h  and output the first element of the rest.
```
The adfix built-in `a` lists first all prefixes in increasing order of length, then suffixes in decreasing order of length.
Thus the output is produced by the shortest prefix that allows it, if any.
If a prefix has no duplicates, the rest of the program fails for it, since every subsequence of equal elements has length 1, and the first element of its tail doesn't exist.
If a prefix has a repeated element, we can choose the length-2 subsequence containing both, and the program returns the latter.
[Answer]
# [Python 3](https://docs.python.org/3/), ~~94~~ 92 bytes
O(n) time and O(1) extra memory.
```
def f(a):
 r=-1
 for i in range(len(a)):t=abs(a[i])-1;r=[r,i+1][a[t]<0>r];a[t]*=-1
 return r
```
[Try it online!](https://tio.run/##PY5BDsIgEEXXcopZgtJEWt204kUIC1RQkoY2w7jw9EhravIXP28yL3/@0GtKXSkPHyBwJ3oGqBvFIEwIEWICdOnp@ehTvYqetLtl7ky0olEDaoMyHpQ1zpC9HK9oh6XtVwV6emMVFPKZ7i77rA3bmVZCt0ZJOEtorfzB0worUZVYtgzYHpcdf0k/Y0zEA9@IEOUL "Python 3 – Try It Online")
[Source of the algorithm](http://www.geeksforgeeks.org/find-duplicates-in-on-time-and-constant-extra-space/).
### Explanation
The basic idea of the algorithm is to run through each element from left to right, keep track of the numbers that have appeared, and returning the number upon reaching a number that has already appeared, and return -1 after traversing each element.
However, it uses a clever way to store the numbers that have appeared without using extra memory: to store them as the sign of the element indexed by the number. For example, I can represent the fact that `2` and `3` has already appeared by having `a[2]` and `a[3]` negative, if the array is 1-indexed.
[Answer]
# [Perl 6](https://perl6.org), 13 bytes
```
*.repeated[0]
```
[Try it](https://tio.run/##K0gtyjH7X1qcqlBmppdszZVbqaCWlllUXKKbUlqQk5mcWJKqYPtfS68otSAVyE6JNoj9D1blkKhgq2Cko2AMRoY6CqY6CkbWXMWJlQpo@jUcEjWtFZQVjLnA@pIg@kzA@oCaDHFoSgJr8svM@Q8A "Perl 6 – Try It Online")
---
## Explanation
* The `*` is in a Term position so the whole statement is a [WhateverCode](https://docs.perl6.org/type/WhateverCode) lambda.
* The `.repeated` is a method that results in every value except for the first time each value was seen.
```
say [2, 3, 3, 3, 1, 5, 2, 3].repeated.perl; # (3, 3, 2, 3).Seq
#   (      3, 3,       2, 3).Seq
```
* `[0]` just returns the first value in the [Seq](https://docs.perl6.org/type/Seq).  
If there is no value [Nil](https://docs.perl6.org/type/Nil) is returned.  
([Nil](https://docs.perl6.org/type/Nil) is the base of the [Failure](https://docs.perl6.org/type/Failure) types, and all types are their own undefined value, so [Nil](https://docs.perl6.org/type/Nil) different than an undefined value in most other languages)
---
Note that since the [implementation of `.repeated`](https://github.com/rakudo/rakudo/blob/51e59eeb5e48917ed3e825947989f91a219ce2cc/src/core/Any-iterable-methods.pm#L1595-L1628) generates a [Seq](https://docs.perl6.org/type/Seq) that means it doesn't start doing any work until you ask for a value, and it only does enough work to generate what you ask for.  
So it would be easy to argue this has at worst *O(n)* time complexity, and at best *O(2)* time complexity if the second value is a repeat of the first.  
Similar can probably be said of memory complexity.
[Answer]
# [J](http://jsoftware.com/), 12 bytes
```
,&_1{~~:i.0:
```
[Try it online!](https://tio.run/##y/r/P03B1kpBRy3esLquzipTz8CKiys1OSNfQcM6TVPNTsFIwRgIDRVMgSxrIDYB8kwVDP//BwA "J – Try It Online")
## Explanation
```
,&_1{~~:i.0:  Input: array M
      ~:      Nub-sieve
          0:  The constant 0
        i.    Find the index of the first occurrence of 0 (the first duplicate)
,&_1          Append -1 to M
    {~        Select the value from the previous at the index of the first duplicate
```
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 20 bytes
```
⊃n/⍨(,≢∪)¨,\n←⎕,2⍴¯1
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qKPdLbOouMSltOD/o67mPP1HvSs0dB51LnrUsUrz0AqdmLxHbRMe9U3VMXrUu@XQekOQhv8KYADTx2Wko2AMRoY6CqY6CkZcmPImYHmgpCG6pLGOkQ4YAwA "APL (Dyalog Unicode) – Try It Online")
`2⍴¯1` negative one **r**eshaped into a length-two list
`⎕,` get input (mnemonic: console box) and prepend to that
`n←` store that in *n*
`,\` prefixes of *n* (lit. cumulative concatenation)
`(`…`)¨` apply the following tacit function to each prefix
 `,` [is] the ravel (just ensures that the prefix is a list)
 `≢` different from
 `∪` the unique elements[?] (i.e. is does the prefix have duplicates?)
`n/⍨` use that to filter *n* (removes all elements until the first for which a duplicate was found)
`⊃` pick the first element from that
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 11 bytes
As per [the new rules](https://codegolf.stackexchange.com/questions/136713/find-the-first-duplicated-element/136797?noredirect=1#comment334435_136713), throws an error if no duplicates exist.
```
⊢⊃⍨⍬⍴⍳∘≢~⍳⍨
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///3y2zqLjEpbTgUduER12LHnU1P@pd8ah3zaPeLY96Nz/qmPGoc1EdiNW74v9/BTCA6VAw0lEwBiNDHQVTHQUjLjR5Yx0jHTDmwtRoAtYI1GUIAA "APL (Dyalog Unicode) – Try It Online")
`⍳⍨` the indices of the first occurrence of each element
`~` removed from
`⍳∘≢` of all the indices
`⍬⍴` reshape that into a scalar (gives zero if no data is available)
`⊃⍨` use that to pick from (gives error on zero)
`⊢` the argument
[Answer]
## APL, 15
```
{⊃⍵[(⍳⍴⍵)~⍵⍳⍵]}
```
Seems like we can return 0 instead of -1 when there are no duplicates, (thanks Adám for the comment). So 3 bytes less.
A bit of description:
```
⍵⍳⍵         search the argument in itself: returns for  each element the index of it's first occurrence
(⍳⍴⍵)~⍵⍳⍵   create a list of all indexes, remove those found in ⍵⍳⍵; i.e. remove all first elements
⊃⍵[...]     of all remaining elements, take the first. If the array is empty, APL returns zero
```
For reference, old solution added -1 to the list at the end, so if the list ended up empty, it would contain -1 instead and the first element would be -1.
```
{⊃⍵[(⍳⍴⍵)~⍵⍳⍵],¯1}
```
Try it on [tryapl.org](http://tryapl.org/?a=%7B%u2283%u2375%5B%28%u2373%u2374%u2375%29%7E%u2375%u2373%u2375%5D%7D2%203%203%201%205%202&run)
[Answer]
# [Retina](https://github.com/m-ender/retina), ~~26~~ 24 bytes
```
1!`\b(\d+)\b(?<=\b\1 .*)
```
[Try it online!](https://tio.run/##K0otycxL/P/fUDEhJkkjJkVbE0jZ29jGJMUYKuhpaf7/b6RgDISGCqYKRgA "Retina – Try It Online") Explanation: `\b(\d+)\b` matches each number in turn, and then the lookbehind looks to see whether the number is a duplicate; if it is the `1`st match is `!` output, rather than the count of matches. Unfortunately putting the lookbehind first doesn't seem to work, otherwise it would save several bytes. Edit: ~~Added 7 bytes to comply with the `-1` return value on no match.~~ Saved 2 bytes thanks to @MartinEnder.
[Answer]
# PHP, ~~56 44 38~~ 32 bytes
```
for(;!${$argv[++$x]}++;);echo$x;
```
Run like this:
```
php -nr 'for(;!${$argv[++$x]}++;);echo$x;' -- 2 3 3 1 5 2;echo
> 3
```
# Explanation
```
for(
  ;
  !${                 // Loop until current value as a variable is truthy
    $argv[++$x]       // The item to check for is the next item from input
  }++;                // Post increment, the var is now truthy
);
echo $x;              // Echo the index of the duplicate.
```
# Tweaks
* Saved 12 bytes by using variables instead of an array
* Saved 6 bytes by making use of the "undefined behavior" rule for when there is no match.
* Saved 6 bytes by using post-increment instead of setting to 1 after each loop
# Complexity
As can be seen from the commented version of the code, the time complexity is linear `O(n)`. In terms of memory, a maximum of `n+1` variables will be assigned. So that's `O(n)`.
[Answer]
# [K (ngn/k)](https://codeberg.org/ngn/k), 11 bytes
```
*(~':?',\)#
```
[Try it online!](https://ngn.codeberg.page/k#eJxLs9LSqFO3slfXidFU5uJKi9YwUjAGQhMFUwUjzViIAIgDEoQLQOTNNGMBn9wMqw==)
```
*(~':?',\)#
 (       )#   keep the elements where the left function returns true
              (when applied to the whole array):
       ,\       prefixes
     ?'         uniquify each
  ~':           is it the same as previous?
*             first element
```
[Answer]
# R, 34 bytes
```
c((x=scan())[duplicated(x)],-1)[1]
```
Cut a few characters off the answer from @djhurio, don't have enough reputation to comment though.
[Answer]
# J, ~~17~~ 16 bytes
```
(*/{_1,~i.&0)@~:
```
### How?
```
(*/{_1,~i.&0)@~:
             @~: returns the nub sieve which is a vector with 1 for the first occurrence of an element in the argument and 0 otherwise
        i.&0     returns the first index of duplication
    _1,~         appends _1 to the index
 */              returns 0 with duplicates (product across nub sieve)
     {           select _1 if no duplicates, otherwise return the index
```
[Answer]
# [R](https://www.r-project.org/), 28 bytes
```
(x=scan())[duplicated(x)][1]
```
[Try it online!](https://tio.run/##K/r/X6PCtjg5MU9DUzM6pbQgJzM5sSQ1RaNCMzbaMPa/KZcxEBpymXIZcf0HAA "R – Try It Online")
[Answer]
## Dyalog APL Classic, 18 chars
Only works in `⎕IO←0`.
```
     w[⊃(⍳∘≢~⍳⍨)w←¯1,⎕]
```
Remove from the list of indices of the elements of the argument with a prepended "-1" the list indices of its nub and then pick the first of what's left. If after the removal there only remains an empty vector, its first element is by definition 0 which is used to index the extended argument producing the desired -1.
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~28~~ 36 bytes
Misunderstood the challenge the first time. `O(n)` time, `O(n)` space.
```
->a{d={};a.find{|e|b=d[e];d[e]=1;b}}
```
[Try it online!](https://tio.run/##KypNqvyfZhvzX9cusTrFtrrWOlEvLTMvpbomtSbJNiU6NdYaRNgaWifV1v4vz8jMSVVITy0p5ipQSItOLUvMUVCJj@VKzUv5H22ko2AMRoY6CqY6CkaxXCAhE7AQkG8YCwA "Ruby – Try It Online")
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), ~~65~~ ~~117~~ 109 bytes
Previous 65 byte solution:
```
r->{for(int a,b=0,z,i=0;;b=a)if((a=b|1<<(z=r[i++]))==b)return z;}
```
New solution. 19 bytes are included for `import java.math.*;`
*-8 bytes thanks to @Nevay*
```
r->{int z,i=0;for(BigInteger c=BigInteger.ZERO;c.min(c=c.setBit(z=r[i++]))!=c;);return z;}
```
[Try it online!](https://tio.run/##dVJNb9swDD3Hv4K7SasjdCm2i@sA6z6AHooB7U4retBs2ZNnS4ZEB02K/PaUkuO5xVLAMCW@R@mJj43cyKXtlWnKvwfd9dYhNJQTncQ/4krX1wZVrVyWvAQH1K347Jzc@hNANZgCtTXi@3FxguPRKdkJOv0urt6mTHjSD79bXUDRSu/hRmqTPCWLY9KjRAobq0voCGJUpU19/wDS1Z4nC6IuUHlkqxQu4vchhY8prHg2IZQYwfCf0xf/pSvrgGmDoCGH8xQaCquMdpfwicLZGYdwXSy/laZWrIl1iwbeByYt9wl9pN7pjUT1Sv5cFa7weqdeyh@7IWzF/rVOuMD@0lqvyvCIWJLCjBN5SnNRtRJvZP/TEs7GFi/XY@QCbTSV8aCXNL6tMIgTQgCFqb13W4@KbhtQUJXB1rBxROjY0Q4W2bEXJ8iVNuXXoSc76cJrU6rHmT9qeeV16M7pEnLdPR5FTSN4GfOxK2Gc11CRaQe3XD@Fc3apzs8z8pXNEw9FPm/Er2@3P7JCdDRaRV4Ir/BKI9vl7p78fuD8XV5kPHMKB2dgl@0P4ZHHbSVk37dbRqLiU/bJ4Rk "Java (OpenJDK 8) – Try It Online")
# Edit
The algorithm in my original program was fine, but the static size of the datatype used meant that it broke fairly quickly once the size went above a certain threshold.
I have changed the datatype used in the calculation to increase the memory limit of the program to accommodate this (using `BigInteger` for arbitrary precision instead of `int` or `long`). However, this makes it debatable whether or not this counts as `O(1)` space complexity.
I will leave my explanation below intact, but I wish to add that I now believe it is impossible to achieve `O(1)` space complexity without making some assumptions.
# Proof
Define `N` as an integer such that `2 <= N` .
Let `S` be a list representing a series of random integers `[x{1}, ..., x{N}]`, where `x{i}` has the constraint `1 <= x{i} <= N`.
The time complexity (in Big-O notation) required to iterate through this list exactly once per element is `O(n)`
The challenge given is to find the first duplicated value in the list. More specifically, we are searching for the first value in `S` that is a duplicate of a previous item on the list.
Let `p` and `q` be the positions of two elements in the list such that `p < q` and `x{p} == x{q}`. Our challenge becomes finding the smallest `q` that satisfies those conditions.
The obvious approach to this problem is to iterate through S and check if our `x{i}` exists in another list `T`:
 If `x{i}` does not exist in `T`, we store it in `T`.
 If `x{i}` does exist in `T`, it is the first duplicate value and therefore the smallest `q`, and as such we return it.
This space efficiency is `O(n)`. 
In order to achieve `O(1)` space complexity while maintaining `O(n)` time complexity, we have to store unique information about each object in the list in a finite amount of space. Because of this, the only way any algorithm could perform at `O(1)` space complexity is if:
 1. N is given an upper bound corresponding to the memory required to store the maximum number of possible values for a particular finite datatype.
 2. The re-assignment of a single immutable variable is not counted against the complexity, only the number of variables (a list being multiple variables).
 3. (Based on other answers) The list is (or at least, the elements of the list are) mutable, and the datatype of the list is preset as a signed integer, allowing for changes to be made to elements further in the list without using additional memory.
1 and 3 both require assumptions and specifications about the datatype, while 2 requires that only the number of variables be considered for the calculation of space complexity, rather than the size of those variables. If none of these assumptions are accepted, it would be impossible to achieve both `O(n)` time complexity and `O(1)` space complexity.
# Explanation
Whoo boy, this one took ~~an embarrassingly long time to think up~~ a bit of brain power.
So, going for the bonus is difficult. We need both to operate over the entire list exactly once **and** track which values we've already iterated over without additional space complexity.
Bit manipulation solves those problems. We initialize our `O(1)` 'storage', a pair of integers, then iterate through the list, OR-ing the ith bit in our first integer and storing that result to the second. 
For instance, if we have `1101`, and we perform an OR operation with `10`, we get `1111`. If we do another OR with `10`, we still have `1101`. 
Ergo, once we perform the OR operation and end up with the same number, we've found our duplicate. No duplicates in the array causes the program to run over and throw an exception.
[Answer]
# Java 8, 82 78 76 bytes No longer viable, 75 67 64 bytes below in edit
As a lambda function:
```
a->{Set<Long>s=new HashSet<>();for(long i:a)if(!s.add(i))return i;return-1;}
```
Probably can be made much smaller, this was very quick.
### Explanation:
```
a->{                                //New lambda function with 'a' as input
    Set<Long>s=new HashSet<>();     //New set
    for(long i:a)                   //Iterate over a
        if(!s.add(i))               //If can't add to s, already exists
            return i;               //Return current value
        return-1;                   //No dupes, return -1
}
```
# \*Edit\*
75 67 64 bytes using the negation strategy:
```
a->{int i=0,j;while((a[j=Math.abs(a[i++])-1]*=-1)<0);return++j;}
```
[Try it online!](https://tio.run/##bVDBbsIwDD03X5FjO9yoSaGASjlNk3bgxBFxyCCFdCWtGpcJIb69Swubpm1RIsfPst/zK@RZhlWtTLF/7/SprhqkhcNYi7pkeWt2qCvDXh6flJC6fSv1ju5KaS1dSW3I1SPeA7Uo0YVzpff05Gr@GhttDpstlc3BBsS7EvrroLLocxAwhnh4SZASz/uGe3ACCUxhBnPgEXAOXACPgY@BT4AnwKfAZ8DnICIQrkeAiEGMQUxAJCCmIGYg5hBHEHOI3cjY3Z7lRv5I1wZprs3@ua2V75JBejMo97wvGxZDAeirQXVQzZLmNKOdDJfXvl1nERTpx1GXyvflpshWEo9MvlmX6NFoG4R8@5SFPFhEQdoobBszGhXprev3vuc0Z7Kuy4vfc/8vdPB4cMlxMsZ65fahc32xqE6sapHVzn8sjf9zJxvcZ95I9wk "Java (OpenJDK 8) – Try It Online")
(-3 bytes thanks to @Nevay)
### Explanation:
```
a->{                                         //New lambda expression with 'a' as input
    int i=0,j;                               //Initialise i and declare j
    while((a[j=Math.abs(a[i++])-1]*=-1)<0);  //Negate to keep track of current val until a negative is found
    return++j;                               //Return value
}
```
Loops over the array, negating to keep track. If no dupes, just runs over and throws an error.
Both of these work on O(n) time and O(n) space complexity.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 4 bytes
```
←Ṡ-u
```
Returns `0` if no input contains no duplicate, [try it online!](https://tio.run/##yygtzv7/qKmx6Nzi8v@P2iY83LlAt/T////RRjoKxmBkqKNgqqNgFAsA "Husk – Try It Online")
### Explanation
```
←Ṡ-u  -- takes a list X as input & returns 0 or first duplicate
 Ṡ-   -- compute X - ...
   u  --       ...   deduplicate X
←     -- get first element or 0 if empty
```
[Answer]
# [Haskell](https://www.haskell.org/), 34 bytes
```
import Data.List
f x=head$x\\nub x
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/PzO3IL@oRMElsSRRzyezuIQrTaHCNiM1MUWlIiYmrzRJoeK/rq4CSEBPQyMmRtNGyw4oqsmVm5iZp2CrUFCUmVeioKKQFm2oY6RjDIRAOvY/AA "Haskell – Try It Online") For an input `x = [2,3,3,1,5,2]`, `nub x` removes duplicates `[2,3,1,5]` and `x\\nub x` removes the elements of `nub x` from x, thus keeping the duplicates `[3,2]`. `head` returns the first element of hat list.
---
Point-free is the same byte count: `head.((\\)<*>nub)`.
[Answer]
# [MATL](https://github.com/lmendo/MATL), 8 bytes
```
&=Rsqf1)
```
Gives an error (without output) if no duplicate exists.
Try at [**MATL Online!**](https://matl.io/?code=%26%3DRsqf1%29&inputs=%5B2%2C+3%2C+3%2C+1%2C+5%2C+2%5D&version=20.2.2)
### Explanation
```
&=   % Implict input. Matrix of all pairwise equality comparisons
R    % Keep the upper triangular part (i.e. set lower part to false)
s    % Sum of each column
q    % Subtract 1
f    % Indices of nonzero values
1)   % Get first. Gives an error is there is none. Implictly display
```
]