https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/
class Solution {
//方法一
// public int findRepeatNumber(int[] nums) {
// int[] flag = new int[nums.length];
// for(int i=0;i<nums.length;i++){
// if(flag[nums[i]]==0){
// flag[nums[i]]=1;
// }else{
// return nums[i];
// }
// }
// return 0;
// }
//方法2 利用hashset
public int findRepeatNumber(int[] nums) {
int repeat=-1;
Set<Integer> set = new HashSet<>();
for(int num:nums){
if(!set.add(num)){
repeat=num;
break;
}
}
return repeat;
}
}