Skip to content

Commit 75c02d9

Browse files
Jeehay28Jeehay28
authored andcommitted
Add contains-duplicate solution using Set in TypeScript
1 parent a17a2d0 commit 75c02d9

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

contains-duplicate/Jeehay28.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Approach 3: HashSet (Using TypeScript Set)
2+
// ⏳ Time Complexity: O(n)
3+
// 💾 Space Complexity: O(n)
4+
5+
function containsDuplicate(nums: number[]): boolean {
6+
7+
const seen = new Set<number>();
8+
9+
for (const num of nums) {
10+
if (seen.has(num)) {
11+
return true;
12+
}
13+
seen.add(num);
14+
}
15+
16+
return false;
17+
18+
};
19+
20+
21+
// Approach 2: Sorting + Scan
22+
// ⏳ Time Complexity: O(n * log(n)) ❌ (Faster than O(n^2), but still not optimal)
23+
// 💾 Space Complexity: O(1)
24+
25+
// function containsDuplicate(nums: number[]): boolean {
26+
27+
// nums.sort();
28+
29+
// for (let i = 0; i < nums.length - 1; i++) {
30+
// if (nums[i] === nums[i + 1]) {
31+
// return true;
32+
// }
33+
// }
34+
35+
// return false;
36+
37+
// };
38+
39+
40+
// Approach 1: Brute Force (O(n^2))
41+
// 🚨⏳ TLE (Time Limit Exceeded)!
42+
// ⏳ Time Complexity: O(n^2) ❌ (Inefficient for large inputs)
43+
// 💾 Space Complexity: O(1) ✅ (Great, no extra memory used!)
44+
45+
// function containsDuplicate(nums: number[]): boolean {
46+
47+
// for (let i = 0; i < nums.length; i++) {
48+
// for (let j = i + 1; j < nums.length; j++) {
49+
// if (nums[i] === nums[j]) {
50+
// return true;
51+
// }
52+
// }
53+
// }
54+
55+
// return false;
56+
57+
// };
58+
59+
60+

0 commit comments

Comments
 (0)