```
public class Solution {
public int findDuplicate(int[] nums) {
int n = nums.length;
int[] flag = new int[n];
for (int i = 0; i < n; ++i) {
flag[i] = 0;
}
for (int j = 0; j < n; ++j) {
if (flag[nums[j]] == 0)
flag[nums[j]] = 1;
else
return nums[j];
}
return -1;
}
}
```