Skip to content

03.数组中重复的数

  • 使用set存储遍历过的数
  • set.add() 返回布尔值,是否包含该值,存在则返回该值
class Solution {
    public int findRepeatNumber(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for(int num:nums){
            if(!set.add(num)){
                return num;
            }
        }
        return -1;
    }
}