×

C++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B.

Example

Input :

a[] = {1, 2, 3, 5, 4};

b[] = {1, 2, 3};

Output: 5 4 

Explanation:

5 and 4 are present in the first array, but not in the second array.

Input :

a[] = {4, 3, 5, 9, 11};

b[] = {4, 9, 3, 11, 10};

Output: 5

Explanation:

5 is present in the first array, but not in the second array.

  1. Method 1

A simple approach is to use two loops and check whether the A[i] is present in array B.

CPP code

#include<bits/stdc++.h>

using namespace std;




void findMissingelement(int a[], int b[],

                 int n, int m) // find missing element in the array B

{

    for (int i = 0; i < n; i++) // outer loop

    {

        int j;

        for (j = 0; j < m; j++) // inner loop

            if (a[i] == b[j]) // if element is found search element in a[i]

                break;




        if (j == m) // if we reach end of the array means element is not found

            cout << a[i] << " "; // print the element

    }

}







int main()

{

    int a[] = { 1, 2, 6, 3, 4, 5 }; // array A

    int b[] = { 2, 4, 3, 1, 0 }; // array B

    int n = sizeof(a) / sizeof(a[0]); // find size of array A

    int m = sizeof(b) / sizeof(b[1]); // find size of array B

    findMissingelement(a, b, n, m);

    return 0;

}

Output

6 5

C code

#include <stdio.h>

void findMissingelement(int a[], int b[],

                 int n, int m) // find missing element in the array B

{

    for (int i = 0; i < n; i++) // outer loop

    {

        int j;

        for (j = 0; j < m; j++) // inner loop

            if (a[i] == b[j]) // if element is found search element in a[i]

                break;




        if (j == m) // if we reach end of the array means element is not found

            printf("%d ",a[i]);  // print the element

    }

}

int main()

{

    int a[] = { 1, 2, 6, 3, 4, 5 }; // array A

    int b[] = { 2, 4, 3, 1, 0 }; // array B

    int n = sizeof(a) / sizeof(a[0]); // find size of array A

    int m = sizeof(b) / sizeof(b[1]); // find size of array B

    findMissingelement(a, b, n, m);

    return 0;

}

Output

6 5
  1. Method 2

The second approach uses the idea of unordered_set.

In this, insert all the elements of array B in the set and iterate in the first array. If the pointer reaches the end in the set, the element is not present.

Unorderd_set

It is a container that stores values in the form of keys.

Internally, a set implements a red black tree and hence the search, insert and delete is O(1).

Syntax

unordered_set<data_type> variable_name;

Code of unordered_set implementation

#include <iostream>

#include <unordered_map>

using namespace std;




int main()

{

         

          unordered_set<int> s;




            s.insert(1);

s.insert(2);

s.insert(3);




          for (auto x : s) // print elements of set

          cout << x  << endl;




}

Output

1

2

3

Method 2 code

#include<bits/stdc++.h>

using namespace std;







void findMissingelement(int a[], int b[],

                int n, int m) // function to find the missing element

{

 

    unordered_set <int> s; // create a set

    for (int i = 0; i < m; i++)

        s.insert(b[i]); // insert array B elements




   // Iterate in array A and search a[i] in set s if set reaches the end the element is not present

    for (int i = 0; i < n; i++)

        if (s.find(a[i]) == s.end())

            cout << a[i] << " "; // print the absent element

}




// Driver code

int main()

{

    int a[] = { 1, 2, 6, 3, 4, 5 };

    int b[] = { 2, 4, 3, 1, 0 };

    int n = sizeof(a) / sizeof(a[0]); // find size of the array a

    int m = sizeof(b) / sizeof(b[1]);// find size of the array b

    findMissingelement(a, b, n, m);

    return 0;

}

Output

6 5

Time complexity - O(n+m)

Space complexity - O(n)


Related Topics

Reverse a Number in C++

In this tutorial, you'll learn how to utilize a C++ program to reverse a number entered by a user at runtime. Because there are various ways to write a C++...

3 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

C++ First Program

Let's write a simple basic program structure of C++, its compilation and its execution (how it runs). This program is compiled using GCC compiler. Open any editor to write C++ program. #include<iostream>   using namespace std;   int main(){       cout<<"Welcome to C++ program"<<endl;   } Output...

2 minutes read.

Structured Binding in C++

Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple...

3 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

C++ Interfaces

The C++ programming language provides programmers with a variety of capabilities and functions. It also enables object-oriented programming, which is essential while working on a project. It will be simple...

7 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.