Non Repeating Numbers | Practice | GeeksforGeeks

Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers.

Example 1:
Input: 
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4 
Explanation:
3 and 4 occur exactly once.

Example 2:
Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.


Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.

Expected Time Complexity: O(N)
Expected Space Complexity: O(1)

Constraints:
1 <= length of array <= 106 
1 <= Elements in array <= 5 * 106
0
vishnu12656 days ago
public int[] singleNumber(int[] nums)
    {
        int xor = 0;
        for(int i=0;i<nums.length;i++){
            xor = xor ^ nums[i];
        }
        int setBit = xor & ~(xor - 1);
        int num1 = 0,num2=0;
        for(int i=0;i<nums.length;i++){
            if((setBit & nums[i]) == setBit){
                num1 = num1 ^ nums[i];
            }
            else{
                num2 = num2 ^ nums[i];
            }
        }
        if(num1>num2){
            return new int[]{num2,num1};
        }
        else{
            return new int[]{num1,num2};
        }
        
    }
+1
ayushrkacholia1 week ago
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;

 // } Driver Code Ends
class Solution
{
public:
    vector<int> singleNumber(vector<int> nums) 
    {
        vector<int> v;
        int res = 0;
        int c =0;
        for(int i=0;i<nums.size();i++){
            res^=nums[i];
        }
        int k =res;
        while(k){
            if((k&1) == 1){
                break;
            }
            c++;
            k = k>>1;
        }
        int j = 1<<c;
        int l = res;
        for(int i=0;i<nums.size();i++){
            if((nums[i]&j)==j){
                l^=nums[i];
            }
        }
        res^=l;
        
        if(res>l){
            
            v.push_back(l);
            v.push_back(res);
        }
        else{
            v.push_back(res);
            v.push_back(l);
        }
        
        return v;
    }
};
Time taken 0.02/2.2
+1
shikhardubey94577568751 week ago
I don't know why my code passes all the test case when i have use Map technique :
Because the map solution has  the TC --→ O ( n logn ) and in the question the tc  given is the o ( n ) and it should not pass the test case .....
But it is fine for me that it passes all test case without using the bit magic ....
 
0
mitradiptamoy1 week ago
 vector<int> singleNumber(vector<int> nums)    {       // Code here.       //O(nlogn)  — T.C
       vector<int>ans;       unordered_set<int>s;       int n=nums.size();       for(auto it:nums){           if(s.find(it)!=s.end()){               s.erase(it);           }           else{               s.insert(it);           }       }       for(int val:s){           ans.push_back(val);       }       sort(ans.begin(),ans.end());       return ans;   }
+1
himanshukug19cs2 weeks ago
java solution
 int xor=0;   for(int i=0;i<nums.length;i++){       xor^=nums[i];   }   //   return xor;       int a=xor;      int b=xor;      int i=1;      while(xor>0&&(xor&1)==0){          xor=xor>>1;          i=i<<1;                }      //System.out.println(b);      int[] ans=new int[2];      for(int j=0;j<nums.length;j++){          if((i&nums[j])==0){              a=a^nums[j];          }          else{              b=b^nums[j];          }      }        ans[0]=a;        ans[1]=b;        Arrays.sort(ans);        return ans;   }
0
stoicamm95
This comment was deleted.
+3
sanikajainece193 weeks ago
vector<int> singleNumber(vector<int> nums)    {       // Code here.       int n=nums.size();       int x=0;       for(int i=0;i<n;i++)       {           x=x^nums[i];       }       int mask=0;       for(int i=0;i<31;i++)       {           if(x&1<<i)           {               mask=1<<i;           }       }       int a=0,b=0;       for(int i=0;i<n;i++)       {           if(nums[i]&mask)           {               a=a^nums[i];           }           else           {               b=b^nums[i];           }       }       vector<int>v;       v.push_back(a);       v.push_back(b);       sort(v.begin(),v.end());       return v;   }
-1
amnks11
This comment was deleted.
0
sanikajainece19
This comment was deleted.
0
adityagagtiwari3 weeks ago
Bit manipulation is fun!!
 
class Solution{   public int findRsb(int n)   {       return (int)(Math.log10(n)/Math.log10(2));   }   public int[] singleNumber(int[] arr)   {       // Code here       int[] result = new int[2];       int wholexor = arr[0];       for(int i=1;i<arr.length;i++)       {           wholexor =wholexor^arr[i];       }       int rsbofwhole = findRsb(wholexor);       int rsbmask = 1<<rsbofwhole;              for(int i=0;i<arr.length;i++)       {           if((arr[i]&rsbmask)>0)           {               result[1] = result[1]^arr[i];           }           else if((arr[i]&rsbmask)==0)           {               result[0] = result[0]^arr[i];           }       }       return result;   }}
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.