Duplicate Elements | Practice | GeeksforGeeks

You are given an array A of size N. The array contains almost distinct elements with some duplicated. You have to print the elements in sorted order which appears more than once. 
Input Format:
The first line of input contains T, denoting the number of testcases. T testcases follow. Each testcase contains two lines of input.  The first line of input contains size of array N. The second line contains N integers separated by spaces.
Output Format:
For each test case, in a new line, print the required answer. If there are no duplicates print -1.
Your Task:
Your task is to complete the function SortedDuplicates(arr, n) which accepts array and its size as an argument. 
Constraints:
1 <= T <= 100
1 <= N <= 100
1 <= Ai <= 106
Examples:
Input:
1
9
3 4 5 7 8 1 2 1 3
Output:
1 3
+1
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION (0.8/2.4 SEC)
public static void SortedDuplicates(int arr[], int n)   {       HashMap<Integer,Integer>map =new HashMap<>();       for(int i=0;i<n;i++)       {           map.put(arr[i],map.getOrDefault(arr[i],0)+1);       }       HashSet<Integer> set=new HashSet<>();       for(int key : map.keySet())       {           if(map.get(key)>1)           {               set.add(key);           }       }       ArrayList<Integer> list=new ArrayList<>(set);       Collections.sort(list);       if(list.isEmpty())       {           System.out.print(-1);       }       else       {       for(int x : list)       {           System.out.print(x+" ");       }       }    }   
0
ankitparashxr3 months ago
java
int count = 0;       ArrayList<Integer> a = new ArrayList<>();       Arrays.sort(arr);       for(int i =1;i<arr.length;i++)       {           if(arr[i]==arr[i-1])           {               if(!a.contains(arr[i]))               {                count++;                System.out.print(arr[i]+" ");                a.add(arr[i]);               }             }       }       if(count==0)       {           System.out.print("-1");       }
0
mainakdeykol4 months ago
public static void SortedDuplicates(int arr[], int n)   {       //Your code here       TreeSet<Integer> tree=new TreeSet<>();       for(int i=0;i<n;i++){           for(int j=i+1;j<n;j++){               if(arr[i]==arr[j]){                   tree.add(arr[i]);               }           }       }       if(tree.isEmpty()){           System.out.print(-1);       }       else{       for(int it:tree)           System.out.print(it+" ");       }    }
+2
badgujarsachin834 months ago
    public static void SortedDuplicates(int arr[], int n)
    {
        //Your code here
        HashSet<Integer> hs = new HashSet<>();
     TreeSet<Integer> tr = new TreeSet<>();
     for(int i=0; i<arr.length; i++){
         int a=arr[i];
         if(hs.contains(a)==false){
             hs.add(a);
         }
         else{
             tr.add(a);
         }
     }
     
     if(tr.size()==0){
         System.out.print("-1");
     }
     else{
         for(int it: tr){
             System.out.print(it+ " ");
          }
     }
      }
-1
rr71951424 months ago
Correct AnswerExecution Time:0.6
 
//----CODE IN JAVA----
HashSet<Integer> hs = new HashSet<>();     TreeSet<Integer> tr = new TreeSet<>();     for(int i=0; i<arr.length; i++){         int a=arr[i];         if(hs.contains(a)==false){             hs.add(a);         }         else{             tr.add(a);         }     }          if(tr.size()==0){         System.out.print("-1");     }     else{         for(int it: tr){             System.out.print(it+ " ");          }     }
-1
Aaditya Burujwale1 year ago
Aaditya Burujwale
Java Easy Solution - TC - O(n)
-1
Amir Ansari1 year ago
Amir Ansari
Correct AnswerExecution Time:0.88
public static void SortedDuplicates(int arr[], int n)    {        //Your code here         Arrays.sort(arr);        Map<integer, integer=""> map = new TreeMap<integer,integer>();
         for(int i = 0; i < n ; i++){              if(map.containsKey(arr[i])){                 map.put(arr[i],map.get(arr[i])+1);               }              else{                  map.put(arr[i],1);               }
         }             String strSorted="";             boolean isSorted = false;              Set<integer> set = new TreeSet<integer>();
             for(int i = 0; i < n; i++){                    if(map.get(arr[i]) > 1){                       isSorted = true;                       set.add(arr[i]);                    }
             }
              if(!isSorted)               System.out.print("-1");             else{                 Iterator it = set.iterator();                 while(it.hasNext()){                     System.out.print(it.next() +" ");                 }             }
     }
-1
Sanjit Paul1 year ago
Sanjit Paul
Easy Java solutionhttps://ide.geeksforgeeks.o...
-1
KASHIF AHMAD2 years ago
KASHIF AHMAD
Dont use new line or println anywhereIn java usually we use println at the end of the function , Since its a functional problem dont use new Line anywhere.
here's the simple java solution:- https://ide.geeksforgeeks.o...
-1
Shobhit Yadav2 years ago
Shobhit Yadav
EASY JAVA SOLUTION
  int t=0;       TreeMap<integer,integer> tm=new TreeMap<>();       for(int i=0;i<n;i++) {="" if(tm.containskey(arr[i]))="" tm.put(arr[i],arr[i]="arr[i]+1);" else="" tm.put(arr[i],0);="" }="" for(map.entry="" it="" :="" tm.entryset())="" {="" int="" value="(int)it.getValue();" int="" key="(int)it.getKey();" if(value-key="">0)           System.out.print(key+" ");           else           t++;       }       if(t==n)       System.out.print("-1");
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.