Skip to content

Commit 2beb39e

Browse files
committed
first solution Contains Duplicate#1
1 parent ae344e8 commit 2beb39e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

contains-duplicate/seungseung88.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 시간 복잡도: O(n) => 배열을 한 번만 순회하므로
2+
// 공간 복잡도: O(n) => 최악의 경우 모든 요소를 객체에 저장하므로
3+
const containsDuplicate = (nums) => {
4+
const indices = {};
5+
6+
// 배열을 한 번 순회
7+
for (let i = 0; i < nums.length; i += 1) {
8+
const num = nums[i];
9+
10+
// 아직 등장하지 않은 숫자라면 객체에 기록
11+
if (!indices[num]) {
12+
indices[num] = 1;
13+
} else {
14+
// 이미 등장한 숫자라면 중복이므로 true 반환
15+
return true;
16+
}
17+
}
18+
19+
// 중복이 없으면 false 반환
20+
return false;
21+
};

0 commit comments

Comments
 (0)