Skip to content

Commit 0686417

Browse files
committed
contains duplicate solution
1 parent 8901b1d commit 0686417

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function(nums) {
6+
7+
// ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•: filter + indexOf ์‚ฌ์šฉ => indexOf(),filter() ๊ฐ๊ฐ ์‹œ๊ฐ„๋ณต์žก๋„ O(n) ๋‘ ๊ฐœ๊ฐ€ ์ค‘์ฒฉ์ด๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„ O(n^2)
8+
// Runtime: Time Limit Exceeded ๋ฐœ์ƒ
9+
const method1 = function() {
10+
const filterNums = nums.filter((item,index) => nums.indexOf(item) !== index);
11+
return filterNums.length > 0;
12+
}
13+
14+
// ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•: Set ์‚ฌ์šฉ => nums ๋ฐฐ์—ด์„ set์œผ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ํ•œ๋ฒˆ์”ฉ ํ™•์ธํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
15+
// Runtime: 14ms
16+
const method2 = function() {
17+
const setNums = new Set(nums);
18+
return setNums.size !== nums.length;
19+
}
20+
21+
// ์œ„ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ Set์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์ƒ ํ›จ์”ฌ ๋‚˜์Œ
22+
return method2();
23+
};

0 commit comments

Comments
ย (0)