Skip to content

Commit a6e89af

Browse files
committed
add article
1 parent 7ff8d83 commit a6e89af

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

articles/duplicate-integer.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## 1. Brute Force
2+
3+
```python
4+
class Solution:
5+
def hasDuplicate(self, nums: List[int]) -> bool:
6+
for i in range(len(nums)):
7+
for j in range(i + 1, len(nums)):
8+
if nums[i] == nums[j]:
9+
return True
10+
return False
11+
```
12+
13+
### Time & Space Complexity
14+
15+
* Time complexity: $O(n^2)$
16+
* Space complexity: $O(1)$
17+
18+
## 2. Sorting
19+
20+
```python
21+
class Solution:
22+
def hasDuplicate(self, nums: List[int]) -> bool:
23+
nums.sort()
24+
for i in range(1, len(nums)):
25+
if nums[i] == nums[i - 1]:
26+
return True
27+
return False
28+
```
29+
30+
### Time & Space Complexity
31+
32+
* Time complexity: $O(n \log n)$
33+
* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm.
34+
35+
## 3. Hash Set
36+
37+
```python
38+
class Solution:
39+
def hasDuplicate(self, nums: List[int]) -> bool:
40+
seen = set()
41+
for num in nums:
42+
if num in seen:
43+
return True
44+
seen.add(num)
45+
return False
46+
```
47+
48+
### Time & Space Complexity
49+
50+
* Time complexity: $O(n)$
51+
* Space complexity: $O(n)$
52+
53+
## 4. Hash Set Length
54+
55+
```python
56+
class Solution:
57+
def hasDuplicate(self, nums: List[int]) -> bool:
58+
return len(set(nums)) < len(nums)
59+
```
60+
61+
### Time & Space Complexity
62+
63+
* Time complexity: $O(n)$
64+
* Space complexity: $O(n)$
65+

0 commit comments

Comments
 (0)