-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontains_duplicate.cpp
More file actions
44 lines (37 loc) · 1020 Bytes
/
contains_duplicate.cpp
File metadata and controls
44 lines (37 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// C++ program to find duplicate from an array using
// unordered_set
#include <bits/stdc++.h>
using namespace std;
// Print duplicates in arr[0..n-1] using unordered_set
void printDuplicates(int arr[], int n)
{
// declaring unordered sets for checking and storing
// duplicates
unordered_set<int> intSet;
unordered_set<int> duplicate;
// looping through array elements
for (int i = 0; i < n; i++)
{
// if element is not there then insert that
if (intSet.find(arr[i]) == intSet.end())
intSet.insert(arr[i]);
// if element is already there then insert into
// duplicate set
else
duplicate.insert(arr[i]);
}
// printing the result
cout << "Duplicate item are : ";
unordered_set<int> :: iterator itr;
// iterator itr loops from begin() till end()
for (itr = duplicate.begin(); itr != duplicate.end(); itr++)
cout << *itr << " ";
}
// Driver code
int main()
{
int arr[] = {1, 5, 2, 1, 4, 3, 1, 7, 2, 8, 9, 5};
int n = sizeof(arr) / sizeof(int);
printDuplicates(arr, n);
return 0;
}