数组中重复的数字

4 阅读1分钟

1.数组中重复的数字 - 蓝桥云课

    //js中变量用let,const就行,而且数组也是这么表示,写个变量名就可以,不用[]
    const exists = new Set();
    //num.forEach方法不会因为回调函数中的 `return` 语句而停止执行。
    for (const num of nums) {
        if (exists.has(num)) {
            return num; // 当找到重复数字时,立即返回
        }
        exists.add(num);
    }
    return null; // 如果没有找到重复的数字,返回 null
}

module.exports = findRepeatNumber;