Find the element that appears once in sorted array | Practice | GeeksforGeeks

Given a sorted array arr[] of size N. Find the element that appears only once in the array. All other elements appear exactly twice. 
Example 1:
Input:
N = 11
arr[] = {1, 1, 2, 2, 3, 3, 4, 50, 50, 65, 65}
Output: 4
Explanation: 4 is the only element that 
appears exactly once.
 
Your Task:  
You don't need to read input or print anything. Complete the function findOnce() which takes sorted array and its size as its input parameter and returns the element that appears only once. 

Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 105
-105 <= arr[i] <= 105
 
0
heenabaig31717 hours ago
Kkk
0
prathyushab8002 days ago
from collections import Counterclass Solution:   def findOnce(self,arr : list, n : int):       # Complete this function       c=Counter(arr)       for k,v in c.items():           if(v==1):               return k
 
0
patilamrut1793 days ago
class Solution{   findOnce(arr,n){       //code here       if(n===1) return arr[0];
       for(let i=0; i<n; i++) {           if(i%2===0 && (i===n-1 || arr[i]!==arr[i+1])) return arr[i];       }       return -1;   }}
0
sanketmudhulkar983 days ago
C++ easy solution using hashmap
 
 int findOnce(int arr[], int n)   {       unordered_map<int,int>freq;              for(int i=0;i<n;i++)       {           freq[arr[i]]++;       }              for(int i=0;i<n;i++)       {           if(freq[arr[i]]==1)           {               return arr[i];           }       }              return -1;   }
0
harshilrpanchal19984 days ago
superb java solution
 
class Solution {   int findOnce(int arr[], int n)   {       // Complete this function        HashMap<Integer, Integer> map = new HashMap<>();      int ans = 0;
      for (int i=0 ; i < n ; i++){          map.put(arr[i] , map.getOrDefault(arr[i] ,0)+1);      }      for (Map.Entry<Integer ,Integer> entry : map.entrySet()){          if (entry.getValue() == 1){              ans = entry.getKey();              break;          }      }      return ans;          }}
0
mayank180919991 week ago
  int findOnce(int arr[], int n)
    {
        //code here.
        int ans=0;
        unordered_map<int,int>m;
        for(int i=0;i<n;i++){
            m[arr[i]]++;
        }
        for(int i=0;i<n;i++){
            if(m[arr[i]]==1){
                ans=arr[i];
            }
        }
     return ans;
    }
0
prabhakarsati294261 week ago
 def findOnce(self,arr : list, n : int):       # Complete this function       x = 0       for i in range(n):           x = x^arr[i]       return x
0
prabhakarsati29426
This comment was deleted.
+1
aloksinghbais021 week ago
C++ solution having time complexity as O(log(N)) and space complexity as O(1) is as follows :- 
 
Execution Time :- 0.21 / 1.37 sec
 
int findOnce(int arr[], int n){       int s = 0;       int e = n-1;       int ans = -1;       while(s <= e){           int mid = (s+e)/2;                      if((mid + 1 < n && arr[mid+1] == arr[mid]) || (mid - 1 >= 0 && arr[mid-1] == arr[mid])){               if(mid+1 < n && arr[mid+1] == arr[mid]){                   if(mid & 1){                       ans = mid;                       e = mid - 1;                   }                   else{                       s = mid + 1;                   }               }               else if(mid - 1 >= 0 && arr[mid-1] == arr[mid]){                   if((mid+1) & 1){                       ans = mid;                       e = mid - 1;                   }                   else{                       s = mid + 1;                   }               }           }           else{               ans = mid;               break;           }       }       return (arr[ans]);   }
0
aryapratik8282 weeks ago
int ans=0;       for(int i=0;i<n;i++)       {          ans=ans^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.