Skip to content

Commit

Permalink
Merge pull request Rishabh062#16 from gayatri517/gayatri517_b4
Browse files Browse the repository at this point in the history
duplicate element in array added in C++ folder
  • Loading branch information
Rishabh062 authored Oct 2, 2021
2 parents af09a0a + 0c0e8b1 commit d7fa9cb
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions C++/duplicate_element_in_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Problem Statement: You are given an array of integers 'ARR' containing N elements. Each integer is in the range [1, N-1],
with exactly one element repeated in the array.
Your task is to find the duplicate element.
Time Complexity: O(nlogn)
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int test;
cout<<"Enter total testcases: "<<endl;
cin>>test;
while(test--)
{
int n;
cout<<"Enter total: "<<endl;
cin>>n;
vector<int> v;
for(int i=0;i<n;i++)
{
int temp;
cin>>temp;
v.push_back(temp);
}
unordered_map<int,int> mp;
for(int i=0;i<n;i++)
{
mp[v[i]]++;
}
for(auto itr: mp)
{
if(itr.second>1)
{
cout<<"The duplicate element is: "<<itr.first<<endl;
}
}
}
return 0;
}

0 comments on commit d7fa9cb

Please sign in to comment.