Skip to content

Commit 1fca4df

Browse files
committed
feat: solve contains-duplicate
1 parent c64c254 commit 1fca4df

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

contains-duplicate/sujeong-dev.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
6+
// for loof: O(n)
7+
var containsDuplicate = function (nums) {
8+
let indices = {};
9+
nums.forEach((num, index) => {
10+
indices[num] = index;
11+
});
12+
13+
for (let i = 0; i < nums.length; i++) {
14+
if (nums[i] in indices && indices[nums[i]] !== i) return true;
15+
}
16+
17+
return false;
18+
};
19+
20+
// size 속성: O(1)
21+
var containsDuplicate = function (nums) {
22+
const indices = new Set(nums);
23+
24+
if (indices.size !== nums.length) return true;
25+
26+
return false;
27+
};

0 commit comments

Comments
 (0)