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++
// 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; } |
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 |
100
Time Complexity: O(N)
Space Complexity: O(1)
Recommended Posts:
- Find index of an extra element present in one sorted array
- Find last element after deleting every second element in array of n integers
- Find an element in array such that sum of left array is equal to sum of right array
- Find element in array that divides all array elements
- Find if array has an element whose value is half of array sum
- Find the maximum element in the array other than Ai
- Find an element in Bitonic array
- Find closest value for every element in array
- Find Second largest element in an array
- Find the first repeating element in an array of integers
- Find the element that appears once in a sorted array
- Find the element whose multiplication with -1 makes array sum 0
- Find first and last positions of an element in a sorted array
- Find lost element from a duplicated array
- Find closest smaller value for every element in array
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.