Skip to content

Commit 829d5a4

Browse files
committed
solve : DaleStudy#217 (Contains Duplicate) with Java
1 parent 8901b1d commit 829d5a4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

contains-duplicate/kdh-92.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public boolean containsDuplicate(int[] nums) {
3+
/**
4+
* Constraints
5+
* - 1 <= nums[] <= 10^5
6+
* - -10^9 <= nums[i] <= 10^9
7+
*
8+
* Output
9+
* - true : 중복 값 존재
10+
* - false : 모든 값이 유일
11+
*/
12+
13+
// 해결법 1 (HashMap 방식 - HashSet 유사)
14+
// 시간복잡도: O(N), 공간 복잡도 : O(N)
15+
Map<Integer, Integer> countMap = new HashMap<>();
16+
17+
for (int num: nums) {
18+
int count = countMap.getOrDefault(num, 0) + 1;
19+
if (count > 1) return true;
20+
countMap.put(num, count);
21+
}
22+
23+
return false;
24+
25+
// 해결법 2 (정렬)
26+
// 시간 복잡도 : O(N log N), 공간 복잡도 : O(1)
27+
Arrays.sort(nums);
28+
29+
for (int i = 0; i < nums.length - 1; i++) {
30+
if (nums[i] == nums[i + 1]) return true;
31+
}
32+
33+
return false;
34+
}
35+
}

0 commit comments

Comments
 (0)