Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include <bits/stdc++.h>
- using namespace std;
- // } Driver Code Ends
- class Solution{
- public:
- vector<int> duplicates(long long arr[], int n) {
- // code here
- vector<int> ans;
- sort(arr,arr+n);
- for(int i=0;i<n-1;i++)
- {
- if(arr[i]==arr[i+1])
- {
- if(ans.size()>=1)
- {
- if(ans[ans.size()-1]!=arr[i])
- {
- ans.push_back(arr[i]);
- }
- }
- else
- {
- ans.push_back(arr[i]);
- }
- }
- }
- if(ans.size()==0)
- {
- return {-1};
- }
- return ans;
- }
- };
- //{ Driver Code Starts.
- int main() {
- int t;
- cin >> t;
- while (t-- > 0) {
- int n;
- cin >> n;
- long long a[n];
- for (int i = 0; i < n; i++) cin >> a[i];
- Solution obj;
- vector<int> ans = obj.duplicates(a, n);
- for (int i : ans) cout << i << ' ';
- cout << endl;
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment