Find all duplicates of array
vector<int> duplicates(int arr[], int n) {
vector<int> dup(n); //create a duplicate of array store occurance of element
vector<int> ans; //store ans which occurs more than one times
for(int i=0;i<n;i++){
if(dup[arr[i]]==1)
{ans.push_back(arr[i]);}
dup[arr[i]]++;
}
if(ans.size()){
sort(ans.begin(), ans.end());
return ans;
}
return {-1};
}
Comments
Post a Comment