Find extra element in the second array

Given two arrays A[] and B[]. The second array B[] contains all the elements of A[] except for 1 extra element. The task is to find that extra element.

Examples:

Input: A[] = { 1, 2, 3 }, B[] = {1, 2, 3, 4}
Output: 4
Element 4 is not present in array



Input: A[] = {10, 15, 5}, B[] = {10, 100, 15, 5}
Output: 100

Naive approach: Run nested loops and find the element in B[] which is not present in A[]. The time complexity of this approach will be O(n2).

Efficient approach: If all the elements of the A[] and B[] are XORed together then every element of A[] will give 0 with its occurrence in B[] and the extra element say X when XORed with 0 will give (X XOR 0) = X which is the result.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the extra
// element in B[]
int extraElement(int A[], int B[], int n)
{
  
    // To store the result
    int ans = 0;
  
    // Find the XOR of all the element
    // of array A[] and array B[]
    for (int i = 0; i < n; i++)
        ans ^= A[i];
    for (int i = 0; i < n + 1; i++)
        ans ^= B[i];
  
    return ans;
}
  
// Driver code
int main()
{
    int A[] = { 10, 15, 5 };
    int B[] = { 10, 100, 15, 5 };
    int n = sizeof(A) / sizeof(int);
  
    cout << extraElement(A, B, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG
{
      
    // Function to return the extra 
    // element in B[] 
    static int extraElement(int A[], int B[], int n) 
    
      
        // To store the result 
        int ans = 0
      
        // Find the XOR of all the element 
        // of array A[] and array B[] 
        for (int i = 0; i < n; i++) 
            ans ^= A[i]; 
        for (int i = 0; i < n + 1; i++) 
            ans ^= B[i]; 
      
        return ans; 
    
      
    // Driver code 
    public static void main (String[] args)
    
        int A[] = { 10, 15, 5 }; 
        int B[] = { 10, 100, 15, 5 }; 
        int n = A.length; 
      
        System.out.println(extraElement(A, B, n)); 
    
}
  
// This code is contributed by kanugargng

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
# Function to return the extra
# element in B[]
def extraElement(A, B, n):
  
    # To store the result
    ans = 0;
  
    # Find the XOR of all the element
    # of array A[] and array B[]
    for i in range(n):
        ans ^= A[i];
    for i in range(n + 1):
        ans ^= B[i];
  
    return ans;
  
# Driver code
A = [ 10, 15, 5 ];
B = [ 10, 100, 15, 5 ];
n = len(A);
  
print(extraElement(A, B, n));
  
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
      
class GFG
{
      
    // Function to return the extra 
    // element in B[] 
    static int extraElement(int []A, 
                            int []B, int n) 
    
      
        // To store the result 
        int ans = 0; 
      
        // Find the XOR of all the element 
        // of array A[] and array B[] 
        for (int i = 0; i < n; i++) 
            ans ^= A[i]; 
        for (int i = 0; i < n + 1; i++) 
            ans ^= B[i]; 
      
        return ans; 
    
      
    // Driver code 
    public static void Main (String[] args)
    
        int []A = { 10, 15, 5 }; 
        int []B = { 10, 100, 15, 5 }; 
        int n = A.Length; 
      
        Console.WriteLine(extraElement(A, B, n)); 
    
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

100

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : kanugargng, 29AjayKumar