forked from Rishabh062/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Rishabh062#16 from gayatri517/gayatri517_b4
duplicate element in array added in C++ folder
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |