Skip to content

Commit 172cc03

Browse files
Create find-duplicates-in-an-array
1 parent 0bc700f commit 172cc03

File tree

1 file changed

+179
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)