Single Number | Practice | GeeksforGeeks

Given an array Arr of positive integers of size N where every element appears even times except for one. Find that number occuring odd number of times.
Example 1:
Input: 
N = 5
Arr[] = {1, 1, 2, 2, 2}
Output: 2
Explanation: In the given array all
element appear three times except 2
which appears once.
Example 2:
Input: 
N = 7
Arr[] = {8, 8, 7, 7, 6, 6, 1}
Output: 1
Explanation: In the given array all
element appear three times except 1
which appears once.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function getSingle() which takes the array of integers arr[] and n as parameters and returns a integer denoting the answer.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 107
0 <= Arri <= 108
 
+1
mayank180919993 hours ago
	int getSingle(int arr[], int n) {
	    // code here
	    map<int,int>m;
	    for(int i=0;i<n;i++){
	        m[arr[i]]++;
	    }
	    for(auto it:m){
	        int a=it.second;
	        if(a%2!=0){
	            return it.first;
	        }
	    }
	  return 0;
	}
0
prasadchaskar2 months ago
Simple Python Solution:
def getSingle(self,arr, n):
        # code here
        d = {}
        num = 0
        for i in arr:
            if i in d:
                d[i] += 1
            else:
                d[i] = 1
        for i in d:
            if d[i] % 2 !=0 :
                num = i
        return num
0
gulshan992 months ago
int getSingle(int arr[], int n) {
	    // code here
	    int ans=0;
	    for(int i=0;i<n;i++)
	    {
	      ans= ans^arr[i];
	    }
	    return ans;
	}
0
ankurkasaudhan3 months ago
class Solution {
    int getSingle(int arr[], int n) {
        // code here
        int xor = 0;
        for(int i=0;i<n;i++)
        {
             xor = xor^arr[i];
        }
        return xor;
    }
}
0
deepakchill77333 months ago
int getSingle(int arr[], int n) {
	    // code here
	    map<int,int> m1;
	    for(int i=0;i<n;i++){
	        m1[arr[i]]++;
	    }
	    for(int i=0;i<n;i++){
	        if(m1[arr[i]]%2!=0){
	            return arr[i];
	            break;
	        }
	    }
	   return 0; 
	}
0
abhayg07013 months ago
  int x=0;    for(int i=0;i<n;i++){           x^=arr[i];       }         return x;
0
amonk4 months ago
int getSingle(int arr[], int n) {    // code here    int m=0;    for(int i=0;i<n;i++){        m=m^arr[i];    }return m;}
0
rohitk214 months ago
bool iseven(int n)   {       if(n%2==0)       {           return true;       }       return false;   }int getSingle(int arr[], int n) {    // code here    unordered_map<int,int> m;    for(int i=0;i<n;i++)    {        m[arr[i]]++;    }    int ans=0;    for(auto it : m)    {        if(!iseven(it.second))        {            ans = it.first;        }    }    return ans;}
0
imranwahid5 months ago
Easy C++ solution
+1
pketul22125 months ago
class Solution {
    int getSingle(int arr[], int n) {
        int output = 0;
        
        for(int i = 0; i < n; i++) 
            output ^= arr[i];
            
        return output;
    }
}
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.