Skip to content

Commit a0fc481

Browse files
committed
contains-duplicate solution
1 parent c10802e commit a0fc481

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
*
3+
* @param nums - ์ •์ˆ˜ ๋ฐฐ์—ด
4+
* @returns - ๋ฐฐ์—ด์— ๊ฐ™์€ ๊ฐ’์ด 2๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต ๋˜๋ฉด true ์•„๋‹˜ false
5+
*
6+
* @description
7+
* 1. Set์œผ๋กœ ๊ธธ์ด ๋น„๊ต ํ›„ ๊ธธ์ด์— ๋”ฐ๋ผ ๋ฐ˜ํ™˜ O(n) -> Set ์ƒ์„ฑ ์‹œ ์ž…๋ ฅ๋œ ๋ฐฐ์—ด ์š”์†Œ ์ˆœํšŒ
8+
* 2. Map์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒดํฌ ๋งŒ์•ฝ has ์‹œ true ๋ฐ˜ํ™˜ O(n)
9+
* 3. Map, Set ์—†์ด ๊ฐ์ฒด๋กœ ์ฒดํฌ O(n)
10+
*/
11+
12+
function containsDuplicate(nums: number[]): boolean {
13+
return nums.length !== new Set(nums).size ? true : false;
14+
}
15+
16+
// function containsDuplicate(nums: number[]): boolean {
17+
// const map = new Map();
18+
// for (const num of nums) {
19+
// if (map.has(num)) {
20+
// return true;
21+
// }
22+
// map.set(num, 1);
23+
// }
24+
// return false;
25+
// }
26+
27+
// function containsDuplicate(nums: number[]): boolean {
28+
// const obj: Record<number, number> = {};
29+
30+
// for (const num of nums) {
31+
// obj[num] = (obj[num] || 0) + 1;
32+
// }
33+
34+
// return Object.keys.length !== nums.length ? true : false;
35+
// }
36+
37+
const exam01 = containsDuplicate([1, 2, 3, 1]);
38+
const exam02 = containsDuplicate([1, 2, 3, 4]);
39+
40+
console.log(exam01, exam02);

0 commit comments

Comments
ย (0)