Exceptionally odd | Practice | GeeksforGeeks

Given an array of N positive integers where all numbers occur even number of times except one number which occurs odd number of times. Find the exceptional number.
Example 1:
Input:
N = 7
Arr[] = {1, 2, 3, 2, 3, 1, 3}
Output: 3


Example 2:
Input:
N = 7
Arr[] = {5, 7, 2, 7, 5, 2, 5}
Output: 5


Your Task:
You don't need to read input or print anything. Your task is to complete the function getOddOccurrence() which takes arr[] and n as input parameters and returns the exceptional number.

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

Constraints:
1 ≤ N ≤ 105
1 ≤ arr[i] ≤ 106
 
0
rahulvishwas26022 days ago
   sort(arr,arr+n);       int count=1;       for(int i=0;i<n;i++){           if(arr[i]==arr[i+1]){               count++;           }           else{               if(count%2==0){                   count=1;                   continue;               }               else               {                   return arr[i];               }           }       }       
0
mehraparthak02 days ago
int getOddOccurence(int arr[],int n) {
int xorsum=0;
for(int i=0;i<n;i++){
xorsum=xorsum^arr[i];
}
return xorsum;
}
+1
harshscode5 days ago
       map<int,int> m;       for(int i=0;i<n;i++)       m[arr[i]]++;       for(auto i:m)       if((i.second%2!=0))       return i.first;
0
yadavashish8828441 week ago
Fast and Easy Bit Manipulation
int xor = 0;       for(int i : arr){           xor = xor ^ i;       }       return xor;
0
kerim21 week ago
Javascript - fast solution 0.33sTime Complexity: O(n)
 
getOddOccurrence(arr,n){
   //code here
   var hashMap = {};
   for(let i = 0; i < n; i++){
       hashMap[arr[i]]? delete hashMap[arr[i]] : hashMap[arr[i]]=1;
   }
   return Object.keys(hashMap)[0];
}
0
atif836142 weeks ago
JAVA SOLUTION :
class Solution {
    int getOddOccurrence(int[] arr, int n) {
        if(n==0){
            return 0;
        }
        HashMap<Integer,Integer> map=new HashMap<>();
        for(int i=0;i<n;i++){
            if(map.containsKey(arr[i])){
                int val=map.get(arr[i]);
                map.put(arr[i],val+1);
            }
            else{
                map.put(arr[i],1);
            }
        }
        for(Integer a: map.keySet()){
            if(map.get(a)%2!=0){
                return a;
            }
        }
        return -1;
    }
}
+1
harshneolia3 weeks ago
class Solution{   public:   int getOddOccurrence(int arr[], int n) {       int x=0;       for(int i=0; i<n;++i)       {           x = x^arr[i];       }           return x;        }};
+1
mohitsalvi1 month ago
class Solution{   public:   int getOddOccurrence(int arr[], int n) {       unordered_map<int,int> mp;              for(int i=0;i<n;i++){           mp[arr[i]]++;       }              for(int i=0;i<n;i++){           if(mp[arr[i]]%2!=0){               return arr[i];           }       }   }};
0
dabhideep441 month ago
// 0.9 seconds
int getOddOccurrence(int[] arr, int n) {       Arrays.sort(arr);       for (int i = 0; i < n; i++) {           if (i < n - 1) {               if (arr[i] != arr[i + 1]) {                   return arr[i];               } else {                   i++;               }           } else {               return arr[n - 1];           }       }       return -1;   }
0
hasnainraza1998hr1 month ago
int getOddOccurrence(int arr[], int n) {       int oddValue = 0;       for(int i=0;i<n;i++){           oddValue ^= arr[i];       }       return oddValue;   }
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.