Skip to content

Commit 1f57f1a

Browse files
committed
added a program in arrays
1 parent 4e5d075 commit 1f57f1a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Arrays/duplicates_in_array.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
vector<int> duplicates(int arr[], int n);
4+
int main()
5+
{
6+
int t;
7+
cin >> t;
8+
while (t-- > 0)
9+
{
10+
int n;
11+
cin >> n;
12+
int a[n];
13+
for (int i = 0; i < n; i++)
14+
cin >> a[i];
15+
vector<int> ans = duplicates(a, n);
16+
for (int i : ans)
17+
cout << i << ' ';
18+
cout << endl;
19+
}
20+
return 0;
21+
}
22+
23+
vector<int> duplicates(int a[], int n)
24+
{
25+
map<int, int> mp;
26+
vector<int> res;
27+
int index, flag = 0;
28+
for (index = 0; index < n; index++)
29+
mp[a[index]]++;
30+
for (auto i : mp)
31+
{
32+
if (i.second > 1)
33+
{
34+
res.push_back(i.first);
35+
flag = 1;
36+
}
37+
}
38+
if (!flag)
39+
{
40+
res.push_back(-1);
41+
}
42+
return res;
43+
}

0 commit comments

Comments
 (0)