Skip to content

Commit e60c0f4

Browse files
committed
Contains Duplicate solution
1 parent ee1ad8e commit e60c0f4

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

contains-duplicate/kimyoung.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var containsDuplicate = function (nums) {
2+
let set = new Set(); // create a set to keep track of items within the nums array
3+
for (const el of nums) set.add(el); // add items to the set, duplicates will automatically be ignored (set vs map)
4+
return set.size !== nums.length; // compare the length of nums array and the size of the set, which shows if there's a duplicate or not
5+
};
6+
7+
// test cases
8+
console.log(containsDuplicate([])); // false
9+
console.log(containsDuplicate([1, 2, 3, 1])); // true
10+
console.log(containsDuplicate([1, 2, 3, 4])); // false

0 commit comments

Comments
 (0)