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
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)