|
1 |
| -**Problem statement:** |
| 1 | +# **Contains Duplicate** |
| 2 | + |
| 3 | +## **Problem Statement** |
2 | 4 | 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.
|
3 | 5 |
|
4 |
| -## Examples: |
5 |
| -Example 1: |
| 6 | +--- |
6 | 7 |
|
7 |
| -Input: nums = [1,1,1,3,3,4,3,2,4,2] |
8 |
| -Output: true |
| 8 | +## **Examples** |
9 | 9 |
|
10 |
| -Example 2: |
| 10 | +### Example 1: |
| 11 | +- **Input**: `nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]` |
| 12 | +- **Output**: `true` |
11 | 13 |
|
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` |
14 | 17 |
|
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 | +--- |
17 | 19 |
|
18 |
| -1. Create an empty set or an object to store the elements. |
| 20 | +## **Algorithmic Approach (Using Set or Object)** |
19 | 21 |
|
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: |
21 | 23 |
|
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`. |
23 | 30 |
|
24 |
| -4. Otherwise add the current element to the set or an object. |
| 31 | +--- |
25 | 32 |
|
26 |
| -5. After the for loop, return `false` to indicate there are no duplicate elements exist in the array. |
| 33 | +## **Time and Space Complexity** |
27 | 34 |
|
| 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. |
28 | 37 |
|
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. |
31 | 40 |
|
32 |
| -Here, we use any additional datastructure like set or object. Hence, the space complexity will be O(n). |
| 41 | +--- |
0 commit comments