Skip to content

Commit 0bcac63

Browse files
committed
contains duplicate solution
1 parent f291ae8 commit 0bcac63

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

β€Žcontains-duplicate/Yn3-3xh.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
[λ¬Έμ œν’€μ΄]
3+
time: O(N), space: O(N)
4+
- 같은 μˆ˜κ°€ ν•˜λ‚˜λΌλ„ 있으면 true
5+
- λͺ¨λ“  μˆ˜κ°€ λ‹€λ₯΄λ©΄ false
6+
7+
[회고]
8+
ArrayList vs Set
9+
ArrayList: O(N^2), 맀번 리슀트λ₯Ό μ²˜μŒλΆ€ν„° 검색해야 ν•˜λ©°, n번 반볡
10+
Set : O(N) , λ‚΄λΆ€μ μœΌλ‘œ ν•΄μ‹œ ν…Œμ΄λΈ”μ„ μ‚¬μš©ν•˜μ—¬ 쀑볡 확인을 O(1)에 μˆ˜ν–‰
11+
λ”°λΌμ„œ 쀑볡 κ²€μ‚¬μ—μ„œ Set 더 효율적
12+
13+
set.add()의 return μžλ£Œν˜•μ€ boolean 이닀.
14+
μ΄ν›„μ—λŠ” if(!set.add()) 처럼 μ‚¬μš©ν•΄λ„ 쒋을 λ“―.
15+
*/
16+
class Solution {
17+
public boolean containsDuplicate(int[] nums) {
18+
Set<Integer> set = new HashSet<>();
19+
for (int num : nums) {
20+
if (set.contains(num)) {
21+
return true;
22+
}
23+
set.add(num);
24+
}
25+
return false;
26+
}
27+
}

0 commit comments

Comments
Β (0)