-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathfindDuplicate.cpp
More file actions
39 lines (36 loc) · 946 Bytes
/
findDuplicate.cpp
File metadata and controls
39 lines (36 loc) · 946 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
// duplicates in the given array
#include <bits/stdc++.h>
using namespace std;
// function to find and print duplicates
void printDuplicates(int arr[], int n)
{
// unordered_map to store frequencies
unordered_map<int, int> freq;
for (int i=0; i<n; i++)
freq[arr[i]]++;
bool dup = false;
unordered_map<int, int>:: iterator itr;
for (itr=freq.begin(); itr!=freq.end(); itr++)
{
// if frequency is more than 1
// print the element
if (itr->second > 1)
{
cout << itr->first << " ";
dup = true;
}
}
// no duplicates present
if (dup == false)
cout << "No duplicates present";
}
int main()
{int n=0;
cout <<"Enter the number of element ";
cin >>n;
int arr[n];
cout <<"Enter the elements ";
for(int i=0;i<n;i++)cin>>arr[i];
printDuplicates(arr, n);
return 0;
}