Skip to content

Commit 1b514fc

Browse files
committed
contains duplicate solution
1 parent 2e9b5fe commit 1b514fc

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

contains-duplicate/ykim7.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Input: number[]
2+
// Output: boolean
3+
4+
// 배열은 sorting되어 있지 않음.
5+
// Idea 1 : 이중 for loop으로 모든 값 비교 - O(n^2)
6+
// Idea 2 : nums를 sort 한 후 앞에서부터 두개씩 비교. 정렬 - O(nlogn)
7+
// Idea 3 : Hash table 로 추가하면서 저장. O(n) 검색 O(1) - O(n)
8+
9+
const found_num = new Set();
10+
11+
for (let i = 0; i < nums.length; i++) {
12+
if (found_num.has(nums[i])) {
13+
return true;
14+
} else {
15+
found_num.add(nums[i]);
16+
}
17+
}
18+
return false;

0 commit comments

Comments
 (0)