Find duplicates in an array | Practice | GeeksforGeeks

Given an array a[] of size N which contains elements from 0 to N-1, you need to find all the elements occurring more than once in the given array.
Example 1:
Input:
N = 4
a[] = {0,3,1,2}
Output: -1
Explanation: N=4 and all elements from 0
to (N-1 = 3) are present in the given
array. Therefore output is -1.

Example 2:
Input:
N = 5
a[] = {2,3,1,2,3}
Output: 2 3 
Explanation: 2 and 3 occur more than once
in the given array.
Your Task:
Complete the function duplicates() which takes array a[] and n as input as parameters and returns a list of elements that occur more than once in the given array in sorted manner. If no such element is found, return list containing [-1]. 
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(n).
Note : The extra space is only for the array to be returned.
Try and perform all operation withing the provided array. 
Constraints:
1 <= N <= 105
0 <= A[i] <= N-1, for each valid i
0
shivamsingh0014122 hours ago
class Solution{
  public:
    vector<int> duplicates(int arr[], int n) {
        int freq[n] = {0};
        vector<int> ans;
        
        for(int i=0; i<n; i++) {
            freq[arr[i]]++;
        }
        
        for(int i=0; i<n; i++) {
            if(freq[i] > 1) {
                ans.push_back(i);
            }
        }
        
        if(ans.size() == 0) {
            ans = {-1};
        }
        
        return ans;
    }
};

0
himanshusethin1 day ago
set<int> result;       sort(arr,arr+n);       for(int i =0;i<n-1;i++){       if(arr[i] == arr[i+1]){           result.insert(arr[i]);       }   }   if(result.empty())       return vector<int> {-1};   else        return vector<int> {result.begin(),result.end()};   }
0
yadavbanti4952 days ago
 vector<int> duplicates(int arr[], int n) {       sort(arr,arr+n);       vector<int>v;       set<int>s;       for(int i=0;i<n;i++)       {           if(arr[i]==arr[i+1])           {               s.insert(arr[i]);           }       }       std::copy(s.begin(), s.end(), std::back_inserter(v));       if(v.empty())       {           v.push_back(-1);           return v;       }       return v;   }
 
0
dangwalshubh93 days ago
python soln:
 
class Solution:   def duplicates(self, arr, n):     # code here    dup = []    max = arr[0]    for i in range(n):        if arr[i] > max:            max = arr[i]    H = []    flag = 0    for j in range(max+1):        H.append(0)    for i in range(n):        H[arr[i]]+=1    for j in range(max+1):        if H[j] > 1:            dup.append(j)            flag = 1    if flag == 0 :       dup.append(-1)        return dup
0
riteshthakare11913 days ago
Begineer solution:
vector<int> duplicates(int arr[], int n) {       vector<int> v;       sort(arr,arr+n);              for (int i=0;i<n;i++){           int count=1;           while(i<(n-1) && arr[i+1]==arr[i]){               count++;               i++;           }           if (count>1){               v.push_back(arr[i]);           }       }       if (v.size()==0){           v.push_back(-1);       }              return v;   }
0
mohantyprateekkumar3 days ago
//The thing is we have to use 3 data structrues here, //hashmap(to keep track of frequency) , arraylist(to //return) and treeset(to maintain order).  
 
 
public static ArrayList<Integer> duplicates(int arr[], int n) {       Map<Integer,Integer> map = new HashMap<>();       Set<Integer> set = new TreeSet<>();       for(int i = 0 ; i < n ; i++){           if(map.containsKey(arr[i])){               map.put(arr[i],map.get(arr[i])+1);               if(map.get(arr[i]) >= 2 && !set.contains(arr[i]))                   set.add(arr[i]);           }           else{               map.put(arr[i],1);           }       }       ArrayList<Integer> al = new ArrayList<>(set);       if(al.isEmpty()){           al.add(-1);       }       return al;   }
0
vishal8579943 days ago
class Solution {   public static ArrayList<Integer> duplicates(int arr[], int n) {       // code here       ArrayList<Integer>al=new ArrayList<>();       HashMap<Integer,Integer>hs=new HashMap<>();       for(int i=0;i<n;i++){            hs.put(arr[i],hs.getOrDefault(arr[i],0)+1);       }       for(Map.Entry<Integer,Integer>val:hs.entrySet()){           if(val.getValue()>1){               al.add(val.getKey());           }       }       if(al.size()<=0){           al.add(-1);           return al;       }else{           Collections.sort(al);           return al;       }        }} 
0
ayalaarziphone3 days ago
i see that if i send the input 11you are expecting the result 0. why?
+1
bollineninitish16 days ago
JAVA
class Solution {
   public static ArrayList<Integer> duplicates(int arr[], int n) {
       ArrayList<Integer> a=new ArrayList<>();
       int[] ar=new int[n];
       Arrays.fill(ar,0);
       for(int i=0;i<arr.length;i++){
           ar[arr[i]]++;
       }
      for(int i=0;i<n;i++){
          if(ar[i]>1){
              a.add(i);
          }
      }
      if(a.size()==0){
          a.add(-1);
      }
      return a;
       
   }
}

0
mannukhurana103971 week ago
Java 
 
public static ArrayList<Integer> duplicates(int arr[], int n) {  // nlogn+n      int[] narr = new int[n]; // O(n): space            ArrayList<Integer> lst = new ArrayList<Integer>();            for(int i=0;i<n;i++){             if (narr[arr[i]]==1)               lst.add(arr[i]);           narr[arr[i]]+=1;      }     
       if(lst.isEmpty())            lst.add(-1);       Collections.sort(lst);                      // -------------------------------------------------------------------       // ArrayList do not maintain the insertion order, else this solution is valid           //     for(int i=0;i<n;i++) // O(n)   //         narr[arr[i]]+=1;         //   for(int i=0;i<n;i++) // O(n)   //       if (narr[i]<=2)   //           lst.add(i);
   //     if(lst.isEmpty())   //          lst.add(-1);            return lst;   }
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.