Skip to content

Commit 2693442

Browse files
committed
contains duplicate solution
1 parent a7426ad commit 2693442

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

contains-duplicate/YuuuuuuYu.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Runtime: 14ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 93.59MB
6+
* Space Complexity: O(n)
7+
*
8+
* Approach: HashSet을 사용하여 중복 검사
9+
* - 배열을 순회하면서 각 원소를 HashSet에 저장
10+
* - 이미 존재하는 원소를 발견하면 즉시 true 반환
11+
*/
12+
class Solution {
13+
public boolean containsDuplicate(int[] nums) {
14+
Set<Integer> set = new HashSet<>();
15+
for (int num: nums) {
16+
if (set.contains(num)) {
17+
return true;
18+
}
19+
set.add(num);
20+
}
21+
return false;
22+
}
23+
}

0 commit comments

Comments
 (0)