Skip to content

Commit e50b2af

Browse files
committed
Update contains duplicate markdown
1 parent 5afd66f commit e50b2af

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed
Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
**Problem statement:**
1+
# **Contains Duplicate**
2+
3+
## **Problem Statement**
24
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.
35

4-
## Examples:
5-
Example 1:
6+
---
67

7-
Input: nums = [1,1,1,3,3,4,3,2,4,2]
8-
Output: true
8+
## **Examples**
99

10-
Example 2:
10+
### Example 1:
11+
- **Input**: `nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]`
12+
- **Output**: `true`
1113

12-
Input: nums = [1,2,3,4, 5]
13-
Output: false
14+
### Example 2:
15+
- **Input**: `nums = [1, 2, 3, 4, 5]`
16+
- **Output**: `false`
1417

15-
**Algorithmic Steps(Approach 1&2)**
16-
This problem is solved with an optimal solution using either set or object to find out duplicate elements exists or not. The algorithmic approach can be summarized as follows:
18+
---
1719

18-
1. Create an empty set or an object to store the elements.
20+
## **Algorithmic Approach (Using Set or Object)**
1921

20-
2. Iterate an input array using for-each loop.
22+
This problem can be solved optimally using either a `Set` or an `Object` to detect duplicate elements. Below is the step-by-step approach:
2123

22-
3. If the current element appears in a set or an object, return `true` immediately to indicate duplicate elements exist.
24+
1. Create an empty `Set` or `Object` to store elements from the array.
25+
2. Iterate through the input array using a loop.
26+
3. For each element:
27+
- If the current element already exists in the `Set` or `Object`, return `true` immediately (indicating duplicates exist).
28+
- Otherwise, add the current element to the `Set` or `Object`.
29+
4. If the loop completes without finding any duplicates, return `false`.
2330

24-
4. Otherwise add the current element to the set or an object.
31+
---
2532

26-
5. After the for loop, return `false` to indicate there are no duplicate elements exist in the array.
33+
## **Time and Space Complexity**
2734

35+
- **Time Complexity**:
36+
The time complexity of this algorithm is **O(n)**, where `n` is the number of elements in the array. This is because we iterate through the array once, and operations like adding or checking membership in a `Set` or `Object` are constant time on average.
2837

29-
**Time and Space complexity:**
30-
This algorithm has a time complexity of O(n), where n is the number of elements in an array. This is because we are traversing the array at most once.
38+
- **Space Complexity**:
39+
The space complexity is **O(n)** as we use an additional data structure (`Set` or `Object`) to store elements from the array.
3140

32-
Here, we use any additional datastructure like set or object. Hence, the space complexity will be O(n).
41+
---

0 commit comments

Comments
 (0)