# Find extra element in the second array

• Difficulty Level : Medium
• Last Updated : 26 Apr, 2021

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:
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++

 `// C++ implementation of the approach``#include ``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;``}`

## Java

 `// 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`

## Python3

 `# 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`

## C#

 `// 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`

## Javascript

 ``

Output:

`100`

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

My Personal Notes arrow_drop_up