Interview Bootcamp
Ask Author
Back to course home

0% completed

Vote For New Content
Raúl Fiol
Alternative solution using Set

Raúl Fiol

Mar 26, 2025

I just found another solution using Set(): by copying each element into a new set. Since a set only stores distinct elements, if the number of elements in the input matches the size of the Set, it means there are no duplicates

function containsDuplicate(nums) { if(!nums || nums.length == 0){ return false; } let nums_copy = new Set(); for(let i = 0; i<nums.length;i++){ nums_copy.add(nums[i]); } return nums_copy.size == nums.length ? false:true; }

1

0

Comments
Comments