Skip to content

Commit d1f7750

Browse files
committed
feat : contains duplicate
1 parent 8901b1d commit d1f7750

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

contains-duplicate/ekgns33.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
/*
5+
start 7:06~7:14 PASS
6+
input : integer array
7+
output : return if input contains duplicate
8+
constraint :
9+
1) empty array?
10+
nope. at least one
11+
2) size?
12+
[1, 10^5]
13+
3) sorted?
14+
nope.
15+
4) range of elements in the array?
16+
[-10^9, 10^9] >> max 2 * 10*9 +1
17+
18+
brute force:
19+
ds : array. algo : just nested for-loop
20+
iterate through the array, for index i 0 to n. n indicates the size of input
21+
nested loop fo r index j from i+1 to n
22+
if nums[j] == nums[i] return true;
23+
24+
return false;
25+
26+
time : O(n^2), space : O(1)
27+
28+
better:
29+
ds : hashset. algo : one for-loop
30+
iterate through the array:
31+
if set contains current value:
32+
return true;
33+
else
34+
add current value to set
35+
36+
return false;
37+
time : O(n) space :O(n)
38+
39+
*/
40+
class Solution {
41+
public boolean containsDuplicate(int[] nums) {
42+
Set<Integer> set = new HashSet<>();
43+
for(int num : nums) {
44+
if(set.contains(num)) return true;
45+
set.add(num);
46+
}
47+
return false;
48+
}
49+
}

0 commit comments

Comments
 (0)