Find extra element in the second array - GeeksforGeeks

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: 4 Element 4 is not present in arrayInput: 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++
Java
Python3
C#
Javascript
// 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 codeint 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 implementation of the approachclass 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 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 codeA = [ 10, 15, 5 ];B = [ 10, 100, 15, 5 ];n = len(A); print(extraElement(A, B, n)); # This code is contributed by 29AjayKumar
// C# implementation of the approachusing 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
<script>// Javascript implementation of the approach // Function to return the extra// element in B[]function extraElement(A, B, n){     // To store the result    let ans = 0;     // Find the XOR of all the element    // of array A[] and array B[]    for (let i = 0; i < n; i++)        ans ^= A[i];    for (let i = 0; i < n + 1; i++)        ans ^= B[i];     return ans;} // Driver code    let A = [ 10, 15, 5 ];    let B = [ 10, 100, 15, 5 ];    let n = A.length;     document.write(extraElement(A, B, n)); // This code is contributed by subhammahato348.</script>
100
 
Time Complexity: O(N) Space Complexity: O(1) 
kanugargng
29AjayKumar
subhammahato348
Bitwise-XOR
Arrays
Bit Magic
Competitive Programming
Arrays
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count pairs with given sum
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?