Two Repeated Elements | Practice | GeeksforGeeks

You are given an array of N+2 integer elements. All elements of the array are in range 1 to N. Also, all elements occur once except two numbers which occur twice. Find the two repeating numbers.
Example 1:
Input:
N = 4
array[] = {1,2,1,3,4,3}
Output: 1 3
Explanation: In the given array, 
1 and 3 are repeated two times.
Example 2:
Input:
N = 2
array[] = {1,2,2,1}
Output: 2 1
Explanation: In the given array,
1 and 2 are repeated two times 
and second occurence of 2 comes 
before 1. So the output is 2 1.

Your Task:
The task is to complete the function repeatedElements() which takes array arr[] and an integer N as inputs (the size of the array is N + 2 and elements are in range[1, N]) and finds the two repeated element in the array and return them in a list.
Note: Return the numbers in their order of appearing twice. So, if X and Y are the repeating numbers, and X repeats twice before Y repeating twice, then the order should be (X,Y).
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). 
Constraints: 
2 ≤ N ≤ 105
1 ≤ array[i] ≤ N
0
abhinayabhi141in 8 hours
class Solution { public:   //Function to find two repeated elements.   vector<int> twoRepeated (int arr[], int N) {       // Your code here        vector<int>v;       for(int i=0; i<N+2; i++)       {           if(arr[abs(arr[i])] > 0)           {               arr[abs(arr[i])] = -arr[abs(arr[i])];           }           else           {               v.push_back(abs(arr[i]));           }       }       return v;   }};
0
sarthakponia0005 hours ago
O(n) & O(1)
 
class Solution {
  public:
    //Function to find two repeated elements.
    vector<int> twoRepeated (int arr[], int N) {
        // Your code here
        vector<int>v;
        for(int i=0; i<N+2; i++)
        {
            if(arr[abs(arr[i])] > 0)
            {
                arr[abs(arr[i])] = -arr[abs(arr[i])];
            }
            else
            {
                v.push_back(abs(arr[i]));
            }
        }
        return v;
        
    }
};
 
0
prabhatv964 days ago
// jAVA
public int[] twoRepeated(int arr[], int N)   {       // Your code here       int ans[] = new int[2];       int idx = 0;       for(int i = 0; i<N+2; i++){           arr[arr[i]%(N+1)] += N+1;           if(arr[arr[i]%(N+1)] / (N+1) == 2){               ans[idx] = arr[i] % (N+1);               idx++;           }       }              return ans;   }
 
 
 
-1
avinav26114 days ago
Easy C++ Solution ( 0.33 / 1.72 sec)
 
+1
bhargav3vedi4 days ago
Python Solution
 
def twoRepeated(self, arr , N):
       #Your code here
       res = []
       
       for i in range(len(arr)):
           vl = arr[i]
           
           nvl = vl*-1 if vl < 0 else vl
           
           if arr[nvl] > 0:
               arr[nvl] *= -1
           else:
               res.append(nvl)
               
       return res
-1
prasadmohite1 week ago
vector<int> twoRepeated (int arr[], int N) {       // Your code here       int a[N+1]={0};       vector <int>vec;       for(int i=0;i<(N+2);i++)       {           a[arr[i]]++;           if(a[arr[i]]==2)           {               vec.push_back(arr[i]);           }                     }              return vec;   }
+2
karansinghjoshi2 weeks ago
Solution WITHOUT EXTRA SPACE , WHITHOUT HASHING or MAPPING concept; 
Total Time Taken:
0.69/1.83
This is an Interesting problem when you wish to solve it in given constaraint WITHOUT USING HASHING OR MAPPING
 
The key is STORING FREQUENCY of appearing number without using extra space and THIS CAN BE DONE by using the same array to store frequency;
We just need to add N to the index (i) of which we need to store frequency, every time we encounter (i) .
if we need to store frequency of 5 we may add N to arr[5] and when we do %N we will get the previously stored element; and when we do /N we will get the frequency stored.
 
 int j =0;
        int size = arr.length;
        
        int[] out = new int[2];
        
        for(int i =0; i<size; i++){
        
            arr[(arr[i]%size)]+=size; 
            
           /*arr[i]%size gives the actual number which was earlier stored in ACTUAL ARRAY*/
        //arr[i] is now arr[i]+frequency*(size)
             		
            if(arr[arr[i]%size] / size ==2) out[j++]=(arr[i]%size);
        }
            
        return out;
-1
rajreigns19162 weeks ago
class Solution {
  public:
    //Function to find two repeated elements.
    vector<int> twoRepeated (int arr[], int N) 
    {
        N = N+2;
        int m = N - 1;
        vector<int> ans;
        
        for (int i = 0; i < N; i++) 
        {
            arr[arr[i] % m - 1] += m;
            
            if ((arr[arr[i] % m - 1] / m) == 2)
                ans.push_back(arr[i] % m);
        }
        
        return ans;
    }
};
0
maneshram20Premium2 weeks ago
very easy C++ solution
class Solution {
  public:
    //Function to find two repeated elements.
    vector<int> twoRepeated (int arr[], int N) {
        unordered_map<int,int> mp;
        vector<int> ans;
        for(int i=0;i<N+2;i++){
            mp[arr[i]]++;
            if(mp[arr[i]]==2){
                ans.push_back(arr[i]);
            }
        }
        return ans;
    }
};
-1
thakursahabsingh123 weeks ago
C++ Solution,
 
class Solution { public:   //Function to find two repeated elements.   vector<int> twoRepeated (int arr[], int N) {       // Your code here         vector<int>count(N+1,0);     vector<int>ans;          for(int i=0;i<N+2;i++)     {         count[arr[i]]+=1;         if(count[arr[i]]==2)              ans.push_back(arr[i]);     }
     return ans;   }}; 
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.