×

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

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

rand() and srand() in C / C++

In this tutorial, we'll explore the syntax, usage, and examples of the C++ STL functions rand() and srand(). What exactly is rand()? The C++ STL's built-in rand() function is defined in the...

3 minutes read.

Computing index using Pointers Returned by STL Functions in C++

In this tutorial we will learn how to compute index using pointers which returned by STL functions in C++. Many built-in C++ functions return pointers to memory places that provide...

2 minutes read.

C++ Global Variable

In C++, a global variable is a variable that is defined outside of any function or class and can be accessed by any function or class in the program. Global...

4 minutes read.

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution. In general, a value of '0' indicates...

3 minutes read.

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

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.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

Inheritance in C++ vs Java

Just like we inherit traits from our parents, object-oriented programming has a concept called inheritance. In terms of object-oriented programming, a class's traits and behaviours, or its data and methods,...

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.

Vector Size in C++

What are Vectors?  In the C++ programming language, vectors are run-time sequence containers representing arrays with variable sizes, which are contained within STL (Standard Template Library). They utilize contiguous storage spaces...

6 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

C++ storage classes

Storage Classes are used to characterize a variable's or function's characteristics. These characteristics include scope, visibility, and life-time, which allow us to track the presence of a variable over the...

5 minutes read.

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics...

6 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

C++ Continue

In C++, the continue statement is a useful tool for avoiding specific scenarios without breaking the loop. It is employed inside loops to move directly to the following iteration and...

4 minutes read.

Program to find the GCD of two numbers in C++

Before understanding the program of GCD or HCF, one must know what GCD or HCF is. What is GCD? The GCD is referred to as Greatest Common Divisor. HCF is the other...

8 minutes read.