-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm217.java
More file actions
47 lines (40 loc) · 1.26 KB
/
prblm217.java
File metadata and controls
47 lines (40 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
public class prblm217 {
public static void main(String[] args) {
int[] nums = {1,2,3,1};
System.out.println(new prblm217().containsDuplicate(nums));
int[] nums2 = {1,2,3,4};
System.out.println(new prblm217().containsDuplicate2(nums2));
int[] nums3 = {1,1,1,3,3,4,3,2,4,2};
System.out.println(new prblm217().containsDuplicate3(nums3));
}
public boolean containsDuplicate(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
if (map.size() != i + 1){
return true;
}
}
return false;
}
public boolean containsDuplicate2(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
if (set.contains(num)) {
return true;
}
else{
set.add(num);
}
}
return false;
}
public boolean containsDuplicate3(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
return set.size() != nums.length;
}
}