You are given an array of n+2 elements. All elements of the array are in range 1 to n. All elements occur once except two numbers, which occur twice. Your task is to find the two repeating numbers. code example

Example 1: find duplicate in an array using xor

int DuplicateNumber(int arr[], int size){
    int ans=0;
    for(int i=0;i<size;i++){
        ans= ans ^ arr[i] ; 
    }
    for(int i=0;i<=size-2;i++){
        ans= ans ^ i;
    }
    return ans;
   }

Example 2: find duplicate in an array using xor

int DuplicateNumber(int arr[], int size){
    /* Don't write main().
     * Don't read input, it is passed as function argument.
     * Return output and don't print it.
     * Taking input and printing output is handled automatically.
     */
    
    int ans=0;
    for(int i=0;i<size;i++){
        ans= ans ^ arr[i] ;
       
    }
    for(int i=0;i<=size-2;i++){
        ans= ans ^ i;
       
    }
    return ans;
    

}