day01

找出数组中的重复数字

思路:

遍历数组,遇到重复的即返回,使用集合进行存储,遍历数组,如果遍历到的数字在集合中,则为重复

先构建集合

     重复数字置为-1

遍历数组

   将元素添加到集合中,判断是否添加成功 不成功则为重复数字,并且赋给repeat

class Solution {
    public int findRepeatNumber(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        int repeat = -1;
        for (int num : nums) {
            if (!set.add(num)) {
                repeat = num;
                break;
            }
        }
        return repeat;
    }
}
原文地址:https://www.cnblogs.com/quanmeng/p/12670218.html