Skip to content
26 changes: 26 additions & 0 deletions contains-duplicate/grapefruitgreentealoe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {number[]} nums
* @return {boolean}
*/

/**
* 문제설명: 2개 이상 반복되는 값이 있으면 true, 모두 반복되지 않으면 false.

제한사항
1 <= nums.length <= 10^5
-109 <= nums[i] <= 109
*/

var containsDuplicate = function (nums) {
const numberSet = new Set();
//시간 복잡도 O(n)
for (i of nums) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요! 첫 리뷰 남겨봅니다!

i 변수가 let, const로 선언되어 있지 않아 JS에서 자동으로 전역 변수로 취급되어 글로벌 스코프 오염 문제가 발생할 수 있을 것 같습니다.
지금처럼 단순한 로직에서는 큰 문제가 없어 보이지만, 복잡한 코드에서는 신경 써야 할 것 같습니다~!

if (!numberSet.has(i)) {
//공간복잡도 O(n)
numberSet.add(i);
} else {
return true;
}
}
return false;
};