|
| 1 | +--- |
| 2 | +id: find-duplicates-in-an-array |
| 3 | +title: Find Duplicates in an Array (gfg) |
| 4 | +sidebar_label: 0006 - Find Duplicates in an Array |
| 5 | +tags: |
| 6 | + - Easy |
| 7 | + - Array |
| 8 | + - GeeksforGeeks |
| 9 | + - CPP |
| 10 | + - Python |
| 11 | + - DSA |
| 12 | +description: "This tutorial covers the solution to the Find Duplicates in an Array problem from the GeeksforGeeks website, featuring implementations in Python and C++." |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem Description |
| 16 | + |
| 17 | +Given an array `arr` of size `n` which contains elements in the range from 0 to `n-1`, you need to find all the elements occurring more than once in the given array. Return the answer in ascending order. If no such element is found, return a list containing [-1]. |
| 18 | + |
| 19 | +:::note |
| 20 | + Try and perform all operations within the provided array. The extra (non-constant) space needs to be used only for the array to be returned. |
| 21 | +::: |
| 22 | +## Examples |
| 23 | + |
| 24 | +**Example 1:** |
| 25 | + |
| 26 | +``` |
| 27 | +Input: arr = [2, 3, 1, 2, 3] |
| 28 | +Output: [2, 3] |
| 29 | +Explanation: 2 and 3 occur more than once in the array. |
| 30 | +``` |
| 31 | + |
| 32 | +**Example 2:** |
| 33 | + |
| 34 | +``` |
| 35 | +Input: arr = [0, 1, 2, 3] |
| 36 | +Output: [-1] |
| 37 | +Explanation: No element occurs more than once. |
| 38 | +``` |
| 39 | + |
| 40 | +## Your Task |
| 41 | + |
| 42 | +You don't need to read input or print anything. Your task is to complete the function `duplicates()` which takes the array `arr` as input and returns a list of the duplicate elements in ascending order. |
| 43 | + |
| 44 | +Expected Time Complexity: $O(n)$ |
| 45 | + |
| 46 | +Expected Auxiliary Space: $O(1)$ (excluding the space for the output list) |
| 47 | + |
| 48 | +## Constraints |
| 49 | + |
| 50 | +* `1 ≤ n ≤ 10^5` |
| 51 | +* `0 ≤ arr[i] ≤ n-1` |
| 52 | + |
| 53 | +## Problem Explanation |
| 54 | + |
| 55 | +The problem is to find the duplicate elements in an array of size `n`, where the elements range from 0 to `n-1`. The duplicates should be returned in ascending order. If no duplicates are found, return [-1]. |
| 56 | + |
| 57 | +## Code Implementation |
| 58 | + |
| 59 | +<Tabs> |
| 60 | + <TabItem value="Python" label="Python" default> |
| 61 | + <SolutionAuthor name="@YourUsername"/> |
| 62 | + |
| 63 | + ```py |
| 64 | + class Solution: |
| 65 | + def duplicates(self, arr): |
| 66 | + n = len(arr) |
| 67 | + # Use the array elements as index |
| 68 | + for i in range(n): |
| 69 | + arr[arr[i] % n] += n |
| 70 | + |
| 71 | + # Collect elements that occur more than once |
| 72 | + result = [i for i in range(n) if arr[i] // n > 1] |
| 73 | + |
| 74 | + return result if result else [-1] |
| 75 | + |
| 76 | + # Example usage |
| 77 | + if __name__ == "__main__": |
| 78 | + solution = Solution() |
| 79 | + print(solution.duplicates([2, 3, 1, 2, 3])) # Expected output: [2, 3] |
| 80 | + print(solution.duplicates([0, 1, 2, 3])) # Expected output: [-1] |
| 81 | + ``` |
| 82 | + |
| 83 | + </TabItem> |
| 84 | + <TabItem value="C++" label="C++"> |
| 85 | + <SolutionAuthor name="@YourUsername"/> |
| 86 | + |
| 87 | + ```cpp |
| 88 | + #include <iostream> |
| 89 | + #include <vector> |
| 90 | + #include <algorithm> |
| 91 | + using namespace std; |
| 92 | + |
| 93 | + class Solution { |
| 94 | + public: |
| 95 | + vector<int> duplicates(vector<long long>& arr) { |
| 96 | + int n = arr.size(); |
| 97 | + vector<int> result; |
| 98 | + |
| 99 | + // Use the array elements as index |
| 100 | + for (int i = 0; i < n; i++) { |
| 101 | + arr[arr[i] % n] += n; |
| 102 | + } |
| 103 | + |
| 104 | + // Collect elements that occur more than once |
| 105 | + for (int i = 0; i < n; i++) { |
| 106 | + if (arr[i] / n > 1) { |
| 107 | + result.push_back(i); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (result.empty()) { |
| 112 | + return {-1}; |
| 113 | + } |
| 114 | + |
| 115 | + return result; |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + // Example usage |
| 120 | + void solve() { |
| 121 | + int n; |
| 122 | + cin >> n; |
| 123 | + vector<long long> arr(n); |
| 124 | + for (int i = 0; i < n; i++) { |
| 125 | + cin >> arr[i]; |
| 126 | + } |
| 127 | + |
| 128 | + Solution obj; |
| 129 | + vector<int> ans = obj.duplicates(arr); |
| 130 | + for (int i : ans) { |
| 131 | + cout << i << ' '; |
| 132 | + } |
| 133 | + cout << endl; |
| 134 | + } |
| 135 | + |
| 136 | + int main() { |
| 137 | + int t; |
| 138 | + cin >> t; |
| 139 | + |
| 140 | + while (t--) { |
| 141 | + solve(); |
| 142 | + } |
| 143 | + return 0; |
| 144 | + } |
| 145 | + ``` |
| 146 | + |
| 147 | + </TabItem> |
| 148 | +</Tabs> |
| 149 | + |
| 150 | +## Example Walkthrough |
| 151 | + |
| 152 | +For the array `arr = [2, 3, 1, 2, 3]`: |
| 153 | + |
| 154 | +1. Iterate through the array and use each element as an index. Increase the value at that index by `n` to mark the occurrence. |
| 155 | +2. After the iteration, elements with values greater than `2 * n` indicate duplicates. |
| 156 | +3. Collect such elements and return them in ascending order. |
| 157 | + |
| 158 | +For the array `arr = [0, 1, 2, 3]`: |
| 159 | + |
| 160 | +1. Iterate through the array and use each element as an index. Increase the value at that index by `n` to mark the occurrence. |
| 161 | +2. After the iteration, no elements have values greater than `2 * n`. |
| 162 | +3. Return [-1] since no duplicates are found. |
| 163 | + |
| 164 | +## Solution Logic: |
| 165 | + |
| 166 | +1. Iterate through the array, using each element as an index. |
| 167 | +2. Increase the value at the calculated index by `n`. |
| 168 | +3. After the iteration, check which elements have values greater than `2 * n` to find duplicates. |
| 169 | +4. Collect such elements and return them in ascending order. |
| 170 | + |
| 171 | +## Time Complexity |
| 172 | + |
| 173 | +* The time complexity is $O(n)$, where n is the size of the input array. |
| 174 | + |
| 175 | +## Space Complexity |
| 176 | + |
| 177 | +* The auxiliary space complexity is $O(1)$ because we are not using any extra space proportional to the size of the input array. |
| 178 | + |
| 179 | +## References |
| 180 | + |
| 181 | +- **gfg Problem:** [gfg Problem](https://www.geeksforgeeks.org/problems/find-duplicates-in-an-array/1?page=1&difficulty=Easy&sortBy=submissions) |
| 182 | +- **Solution Author:** [arunimad6yuq](https://www.geeksforgeeks.org/user/arunimad6yuq/) |
0 commit comments