Find the element that appears once | Practice | GeeksforGeeks

Given a sorted array A[] of N positive integers having all the numbers occurring exactly twice, except for one number which will occur only once. Find the number occurring only once.
Example 1:
Input:
N = 5
A = {1, 1, 2, 5, 5}
Output: 2
Explanation: 
Since 2 occurs once, while
other numbers occur twice, 
2 is the answer.
Example 2:
Input:
N = 7
A = {2, 2, 5, 5, 20, 30, 30}
Output: 20
Explanation:
Since 20 occurs once, while
other numbers occur twice, 
20 is the answer.
Your Task:
You don't need to read input or print anything. Your task is to complete the function search() which takes two arguments(array A and integer N) and returns the number occurring only once.
Expected Time Complexity: O(Log(N)).
Expected Auxiliary Space: O(1).
Constraints
0 <   N  <= 10^6
0 <= A[i] <= 10^9
0
velspace011 week ago
Java sol O(log N)
int l=0;
        int r=n-1;
        if(n==1){
            return arr[0];
        }
        if(arr[n-1]!=arr[n-2]){
            return arr[n-1];
        }
        if(arr[0]!=arr[1]){
            return arr[0];
        }
        while(l<=r){
            int mid=l+((r-l)/2);
            if(arr[mid]<arr[mid+1] && arr[mid]>arr[mid-1]){
                return arr[mid];
            }
            else if(mid%2==0 && arr[mid]!=arr[mid+1]){
                r=mid-1;
            }
            else if(mid%2==1 && arr[mid]==arr[mid+1]){
                r=mid-1;
            }
            else{
                l=mid+1;
            }
        }
        return 0;
0
mayank180919992 weeks ago
	int search(int A[], int N){
	    //code
	   int res=0;
	   for(int i=0;i<N;i++){
	       res=res^A[i];
	   }
	  return res;
	}
-1
mayank180919992 weeks ago
	int search(int A[], int N){
	    //code
	   unordered_map<int,int>m;
	   for(int i=0;i<N;i++){
	       m[A[i]]++;
	   }
	   int ans;
	   for(auto it:m){
	       if(it.second==1){
	          ans=it.first;
	          break;
	       }
	   }
	  return ans;
	}
+1
mridulkapoor1234562 weeks ago
//OPTIMAL SOLUTION
TC->O(logN)
SC->O(1)
class Solution{
public:	
	int search(int A[], int N){
	    int lo=0;
	    int hi=N-2;
	    while(lo<=hi){
	        int mid=(lo+hi)>>1;
	        if(A[mid]==A[mid^1])
	            lo=mid+1;
	        else 
	            hi=mid-1;
	    }
	    return A[lo];
	}
};
0
gupta2411sumit3 weeks ago
int search(int arr[], int N){    //code        for( int i = 0 ; i<N ; i+=2)    {        if(arr[i]!=arr[i+1])        {            return arr[i] ;        }    }}
0
2001amitvats4 weeks ago
C++ BEST SOUTION 
 
int search(int A[], int N){   // int res = A[0];    if(A[0]!=A[1]){         return A[0];    }    for(int i = 1; i<N-1; i++){        if(A[i-1]!=A[i]){            if(A[i]!=A[i+1]){                return A[i];            }        }    }    if(A[N-2]!=A[N-1]){        return A[N-1];    }}
0
saikirannagarjuna0071 month ago
SIMPLE JAVA SOLUTION USING XOR 
class Sol{   public static int search(int A[], int N)   {       // your code here       int k=0;       for(int i=0;i<A.length;i++){           k=k^A[i];       }       return k;   }}
 
0
codewithshoaib191 month ago
int s = 0;       int e = N - 1;       if (N == 1) {           return A[0];       }       if (A[0] != A[1]) {           return A[0];       } else if (A[N - 1] != A[N - 2]) {           return A[N - 1];       }       while (s <= e) {           int mid = s + (e - s) / 2;           if (A[mid] < A[mid + 1] && A[mid] > A[mid - 1]) {               return A[mid];           } else if ((A[mid] == A[mid + 1] && (mid) % 2 == 0)                   || (A[mid] == A[mid - 1] && (mid - 1) % 2 == 0)) {               s = mid + 1;           } else {               e = mid - 1;           }       }       return -1;
+1
bobbyprajapati961 month ago
Using XOR : JAVA 
public static int search(int A[], int N)   {       int res=0;       for(int i=0;i<N;i++)       {           res=res^A[i];       }       return res;   }
0
sunghunet2 months ago
import collections
def search(self, A, N):
    a=collections.Counter(A)
    for num , cnt in a.items():
        if cnt==1:
            return num
    return A[0]
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.