Skip to content

Commit a674237

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

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

contains-duplicate/limlimjo.js

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)