We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ee1ad8e commit e60c0f4Copy full SHA for e60c0f4
contains-duplicate/kimyoung.js
@@ -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