mirror of
https://github.com/Rainyy21/framework_note.git
synced 2026-10-11 00:30:29 -04:00
vault backup: 2026-05-30 16:02:33
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 217
|
||||
title: Contains Duplicate
|
||||
difficulty: Easy
|
||||
tags:
|
||||
- array
|
||||
- hash-table
|
||||
status: Solved
|
||||
date_solved: 2026-05-26
|
||||
leetcode_url: https://leetcode.com/problems/contains-duplicate/
|
||||
review_needed: false
|
||||
---
|
||||
|
||||
# 217. Contains Duplicate
|
||||
|
||||
> [!info] **Problem Link**: [LeetCode - Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)
|
||||
|
||||
## 📝 Problem Description
|
||||
Given an integer array `nums`, return `true` if *any value appears at least twice in the array*, and return `false` if every element is distinct.
|
||||
|
||||
---
|
||||
|
||||
### 📥 Example 1
|
||||
> **Input:** `nums = [1,2,3,1]`
|
||||
> **Output:** `true`
|
||||
> **Explanation:** The element 1 occurs at the indices 0 and 3.
|
||||
|
||||
### 📥 Example 2
|
||||
> **Input:** `nums = [1,2,3,4]`
|
||||
> **Output:** `false`
|
||||
|
||||
### 📥 Example 3
|
||||
> **Input:** `nums = [1,1,1,3,3,4,3,2,4,2]`
|
||||
> **Output:** `true`
|
||||
|
||||
---
|
||||
|
||||
## 💡 Approaches & Explanations
|
||||
|
||||
### Approach 1: Hash Set (Length Comparison)
|
||||
The simplest way to check for duplicates in Python is to convert the array `nums` into a set. A set only contains unique elements, so:
|
||||
- If there are duplicates, the length of the set will be less than the length of the array.
|
||||
- If all elements are unique, the lengths will be equal.
|
||||
|
||||
#### 📊 Complexity Analysis
|
||||
- **Time Complexity:** $\mathcal{O}(N)$ where $N$ is the number of elements in the array. Converting an array to a set requires traversing the entire array and inserting each element.
|
||||
- **Space Complexity:** $\mathcal{O}(N)$ as we store up to $N$ unique elements in the set.
|
||||
|
||||
---
|
||||
|
||||
### Approach 2: Hash Set (Early Return / One-Pass) — *Alternative*
|
||||
Instead of converting the entire array to a set, we can iterate through the array and store elements in a set as we go. If we encounter an element that is already in the set, we can return `true` immediately. This avoids processing the rest of the array.
|
||||
|
||||
#### 📊 Complexity Analysis
|
||||
- **Time Complexity:** $\mathcal{O}(N)$ in the worst case (no duplicates). In the best case, it can be $\mathcal{O}(1)$ if a duplicate is found at the beginning.
|
||||
- **Space Complexity:** $\mathcal{O}(N)$ to store the visited elements.
|
||||
|
||||
---
|
||||
|
||||
## 💻 Code Implementations
|
||||
|
||||
### Python3
|
||||
|
||||
#### Option A: Length Comparison (Concise)
|
||||
```python
|
||||
class Solution:
|
||||
def containsDuplicate(self, nums: List[int]) -> bool:
|
||||
return len(nums) != len(set(nums))
|
||||
```
|
||||
|
||||
#### Option B: Early Return (Optimal for large lists with early duplicates)
|
||||
```python
|
||||
class Solution:
|
||||
def containsDuplicate(self, nums: List[int]) -> bool:
|
||||
seen = set()
|
||||
for num in nums:
|
||||
if num in seen:
|
||||
return True
|
||||
seen.add(num)
|
||||
return False
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🧠 Key Takeaways & Lessons
|
||||
- **Hash Set for Uniqueness:** Sets are the go-to data structure when you need to verify uniqueness or look up elements in $\mathcal{O}(1)$ time.
|
||||
- **Early Return Optimization:** While converting the whole list to a set is clean and concise, iterating and returning early when a duplicate is found can save time and memory in practice.
|
||||
- **Time-Space Trade-off:** We use extra space ($\mathcal{O}(N)$ memory) to achieve linear time complexity ($\mathcal{O}(N)$) instead of a brute-force search ($\mathcal{O}(N^2)$).
|
||||
Reference in New Issue
Block a user