-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path217Contains_Duplicate.java
More file actions
45 lines (42 loc) · 1.22 KB
/
217Contains_Duplicate.java
File metadata and controls
45 lines (42 loc) · 1.22 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
//set does not contain elements which are already in the set
class Solution {
public boolean containsDuplicate(int[] nums) {
// if(nums.length == 0){
// return false;
// }
HashSet<Integer> hs = new HashSet<Integer>();
for(int i = 0; i<nums.length; i++){
int temp = hs.size();
hs.add(nums[i]);
//System.out.println(hs.size() +" " + temp);
if(hs.size() == temp){
return true;
}
}
return false;
}
}
// class Solution {
// public boolean containsDuplicate(int[] nums) {
// Set<Integer> set = new HashSet<>();
// for (int a : nums) {
// // If the element is already in the set, the set is unchanged and will return false.
// if (!set.add(a)) {//directly returns
// return true;
// }
// }
// return false;
// }
// }
//sorting
//class Solution {
// public boolean containsDuplicate(int[] nums) {
// Arrays.sort(nums);
// for(int i=0;i<nums.length-1;i++){
// if(nums[i]==nums[i+1]){
// return true;
// }
// }
// return false;
// }
// }