Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Find whether an array is subset of another array using Map
Next article icon

Find missing number in another array which is shuffled copy

Last Updated : 11 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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
Recommended Practice
Missing number in shuffled array
Try It!

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. 

Implementation:

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

Time Complexity:  O(n).
Space Complexity: O(1).

 


Next Article
Find whether an array is subset of another array using Map

A

Ayush Jauhari
Improve
Article Tags :
  • Bit Magic
  • DSA
  • Arrays
  • Bitwise-XOR
Practice Tags :
  • Arrays
  • Bit Magic

Similar Reads

  • Find whether an array is subset of another array using Map
    Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both arrays are distinct.Examples: Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1} Output: arr2[] is a subset o
    7 min read
  • Find the sum of array after performing every query
    Given an array arr[] of size N and Q queries where every query contains two integers X and Y, the task is to find the sum of an array after performing each Q queries such that for every query, the element in the array arr[] with value X is updated to Y. Find the sum of the array after every query. E
    7 min read
  • Find the Target number in an Array
    Finding a number within an array is an operation, in the field of computer science and data analysis. In this article, we will discuss the steps involved and analyze their time and space complexities. Examples: Input: Array: {10, 20, 30, 40, 50} , Target: 30Output: "Target found at index 2" Input: A
    13 min read
  • Find all duplicate and missing numbers in given permutation array of 1 to N
    Given an array arr[] of size N consisting of the first N natural numbers, the task is to find all the repeating and missing numbers over the range [1, N] in the given array. Examples: Input: arr[] = {1, 1, 2, 3, 3, 5}Output: Missing Numbers: [4, 6]Duplicate Numbers: [1, 3]Explanation:As 4 and 6 are
    8 min read
  • Maximum in an array that can make another array sorted
    Given two arrays among which one is almost sorted with one element being in the wrong position making the array unsorted, the task is to swap that element with the maximum element from the second array which can be used to make the first array sorted. Examples: Input: arr1 = {1, 3, 7, 4, 10}, arr2 =
    8 min read
  • Missing in a Sorted Array of Natural Numbers
    Given a sorted array arr[] of n-1 integers, these integers are in the range of 1 to n. There are no duplicates in the array. One of the integers is missing in the array. Write an efficient code to find the missing integer. Examples: Input : arr[] = [1, 2, 3, 4, 6, 7, 8]Output : 5Explanation: The mis
    12 min read
  • Find the smallest missing number
    Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array. Examples: Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3 Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0 Input: {0, 1, 2, 3}, n = 4, m =
    15 min read
  • Find the Missing Number
    Given an array arr[] of size n-1 with distinct integers in the range of [1, n]. This array represents a permutation of the integers from 1 to n with one element missing. Find the missing element in the array. Examples: Input: arr[] = [8, 2, 4, 5, 3, 7, 1]Output: 6Explanation: All the numbers from 1
    12 min read
  • Find indices in a sorted Matrix where a new number can be replaced
    Given a matrix arr[][] which is sorted by the increasing number of elements and a number X, the task is to find the position where the input integer can be replaced with an existing element without disturbing the order of the sorted elements. The matrix is sorted in such a manner that: Every row is
    11 min read
  • Find original array from given array which is obtained after P prefix reversals | Set-2
    Given an array arr[] of size N and an integer P, the task is to find the original array from the array obtained by P prefix reversals where in ith reversal the prefix of size i of the array containing indices in range [0, i-1] was reversed. Note: P is less than or equal to N Examples: Input: arr[] =
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences