Skip to main content

contain duplicate

Link : https://neetcode.io/problems/duplicate-integer

solution 1

let nums = [1, 2, 3, 3, 5, 4, 4, 7, 9, 1];
function hasDuplicate_01(nums) {
const x = {};
for (let i = 0; i < nums?.length; i++) {
if (x.hasOwnProperty(nums[i])) {
return true;
}
x[nums[i]] = nums[i];
}
return false;
}
let res = hasDuplicate_01(nums);
console.log(res);

solution 2

let nums = [1, 2, 3, 3, 5, 4, 4, 7, 9, 1];
function hasDuplicate_02(nums) {
const numsSet = new Set();
for (const num of nums) {
if (numsSet.has(num)) return true;
numsSet.add(num);
}
return false;
}
let res = hasDuplicate_02(nums);
console.log(res);