Skip to content
Related Articles

Related Articles

Improve Article

Find missing number in another array which is shuffled copy

  • Difficulty Level : Easy
  • Last Updated : 30 Apr, 2021

Given an array ‘arr1’ of n positive integers. Contents of arr1[] are copied to another array ‘arr2’, but numbers are shuffled and one element is removed. Find the missing element(without using any extra space and in O(n) time complexity).
Examples : 
 

Input : arr1[] = {4, 8, 1, 3, 7}, 
        arr2[] = {7, 4, 3, 1}
Output : 8

Input : arr1[] = {12, 10, 15, 23, 11, 30}, 
        arr2[] = {15, 12, 23, 11, 30}
Output : 10

 

A simple solution is to one by one consider every element of first array and search in second array. As soon as we find a missing element, we return. Time complexity of this solution is O(n2)
An efficient solution is based on XOR. The combined occurrence of each element is twice, one in ‘arr1’ and other in ‘arr2’, except one element which only has a single occurrence in ‘arr1’. We know that (a Xor a) = 0. So, simply XOR the elements of both the arrays. The result will be the missing number. 
 

C++




// C++ implementation to find the
// missing number in shuffled array
// C++ implementation to find the
// missing number in shuffled array
#include <bits/stdc++.h>
using namespace std;
 
// Returns the missing number
// Size of arr2[] is n-1
int missingNumber(int arr1[], int arr2[],
                                   int n)
{
    // Missing number 'mnum'
    int mnum = 0;
 
    // 1st array is of size 'n'
    for (int i = 0; i < n; i++)
        mnum = mnum ^ arr1[i];
 
    // 2nd array is of size 'n - 1'
    for (int i = 0; i < n - 1; i++)
        mnum = mnum ^ arr2[i];
 
    // Required missing number
    return mnum;
}
 
// Driver Code
int main()
{
    int arr1[] = {4, 8, 1, 3, 7};
    int arr2[] = {7, 4, 3, 1};
    int n = sizeof(arr1) / sizeof(arr1[0]);
    cout << "Missing number = "
        << missingNumber(arr1, arr2, n);
    return 0;
}

Java




// Java implementation to find the
// missing number in shuffled array
 
class GFG
{
    // Returns the missing number
    // Size of arr2[] is n-1
    static int missingNumber(int arr1[],
                             int arr2[],
                                  int n)
    {
        // Missing number 'mnum'
        int mnum = 0;
     
        // 1st array is of size 'n'
        for (int i = 0; i < n; i++)
            mnum = mnum ^ arr1[i];
     
        // 2nd array is of size 'n - 1'
        for (int i = 0; i < n - 1; i++)
            mnum = mnum ^ arr2[i];
     
        // Required missing number
        return mnum;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr1[] = {4, 8, 1, 3, 7};
        int arr2[] = {7, 4, 3, 1};
        int n = arr1.length;
         
        System.out.println("Missing number = "
            + missingNumber(arr1, arr2, n));
    }
}

Python3




# Python3 implementation to find the
# missing number in shuffled array
 
# Returns the missing number
# Size of arr2[] is n - 1
def missingNumber(arr1, arr2, n):
 
    # missing number 'mnum'
    mnum = 0
 
    # 1st array is of size 'n'
    for i in range(n):
        mnum = mnum ^ arr1[i]
 
    # 2nd array is of size 'n - 1'
    for i in range(n - 1):
        mnum = mnum ^ arr2[i]
 
    # Required missing number
    return mnum
 
# Driver Code
arr1 = [4, 8, 1, 3, 7]
arr2= [7, 4, 3, 1]
n = len(arr1)
print("Missing number = ",
    missingNumber(arr1, arr2, n))
         
# This code is contributed by Anant Agarwal.

C#




// C# implementation to find the
// missing number in shuffled array
using System;
 
class GFG
{
    // Returns the missing number
    // Size of arr2[] is n-1
    static int missingNumber(int []arr1,
                             int []arr2,
                                  int n)
    {
        // Missing number 'mnum'
        int mnum = 0;
     
        // 1st array is of size 'n'
        for (int i = 0; i < n; i++)
            mnum = mnum ^ arr1[i];
     
        // 2nd array is of size 'n - 1'
        for (int i = 0; i < n - 1; i++)
            mnum = mnum ^ arr2[i];
     
        // Required missing number
        return mnum;
    }
     
    // Driver Code
    public static void Main ()
    {
        int []arr1 = {4, 8, 1, 3, 7};
        int []arr2 = {7, 4, 3, 1};
        int n = arr1.Length;
         
    Console.Write("Missing number = "
            + missingNumber(arr1, arr2, n));
    }
}
 
// This code is contributed by nitin mittal.

PHP




<?php
// PHP implementation to find the
// missing number in shuffled array
// PHP implementation to find the
// missing number in shuffled array
 
// Returns the missing number
// Size of arr2[] is n-1
function missingNumber($arr1, $arr2,
                                $n)
{
     
    // Missing number 'mnum'
    $mnum = 0;
 
    // 1st array is of size 'n'
    for ($i = 0; $i < $n; $i++)
        $mnum = $mnum ^ $arr1[$i];
 
    // 2nd array is of size 'n - 1'
    for ($i = 0; $i < $n - 1; $i++)
        $mnum = $mnum ^ $arr2[$i];
 
    // Required missing number
    return $mnum;
}
 
    // Driver Code
    $arr1 = array(4, 8, 1, 3, 7);
    $arr2 = array(7, 4, 3, 1);
    $n = count($arr1);
    echo "Missing number = "
        , missingNumber($arr1, $arr2, $n);
         
// This code is contributed by anuj_67.
?>

Javascript




<script>
 
// Javascript implementation to find the
// missing number in shuffled array
// Javascript implementation to find the
// missing number in shuffled array
 
// Returns the missing number
// Size of arr2[] is n-1
function missingNumber(arr1, arr2, n)
{
    // Missing number 'mnum'
    let mnum = 0;
 
    // 1st array is of size 'n'
    for (let i = 0; i < n; i++)
        mnum = mnum ^ arr1[i];
 
    // 2nd array is of size 'n - 1'
    for (let i = 0; i < n - 1; i++)
        mnum = mnum ^ arr2[i];
 
    // Required missing number
    return mnum;
}
 
// Driver Code
    let arr1 = [4, 8, 1, 3, 7];
    let arr2 = [7, 4, 3, 1];
    let n = arr1.length;
    document.write("Missing number = "
        + missingNumber(arr1, arr2, n));
 
</script>

Output:  

Missing number = 8

Complexity: O(n) time complexity and O(1) extra space.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :