GFG App
Open App
Browser
Continue

# Find elements which are present in first array and not in second

Given two arrays, the task is that we find numbers which are present in first array, but not present in the second array.

Examples :

```Input : a[] = {1, 2, 3, 4, 5, 10};
b[] = {2, 3, 1, 0, 5};
Output : 4 10
4 and 10 are present in first array, but
not in second array.

Input : a[] = {4, 3, 5, 9, 11};
b[] = {4, 9, 3, 11, 10};
Output : 5  ```
Recommended Practice

Method 1 (Simple): A Naive Approach is to use two loops and check element which not present in second array.

Implementation:

## C++

 `// C++ simple program to ` `// find elements which are ` `// not present in second array` `#include` `using` `namespace` `std;`   `// Function for finding ` `// elements which are there ` `// in a[]  but not in b[].` `void` `findMissing(``int` `a[], ``int` `b[], ` `                 ``int` `n, ``int` `m)` `{` `    ``for` `(``int` `i = 0; i < n; i++)` `    ``{` `        ``int` `j;` `        ``for` `(j = 0; j < m; j++)` `            ``if` `(a[i] == b[j])` `                ``break``;`   `        ``if` `(j == m)` `            ``cout << a[i] << ``" "``;` `    ``}` `}`   `// 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]);` `    ``int` `m = ``sizeof``(b) / ``sizeof``(b[1]);` `    ``findMissing(a, b, n, m);` `    ``return` `0;` `}`

## Java

 `// Java simple program to ` `// find elements which are ` `// not present in second array` `class` `GFG ` `{` `    `  `    ``// Function for finding elements ` `    ``// which are there in a[] but not` `    ``// in b[].` `    ``static` `void` `findMissing(``int` `a[], ``int` `b[], ` `                            ``int` `n, ``int` `m)` `    ``{` `        ``for` `(``int` `i = ``0``; i < n; i++)` `        ``{` `            ``int` `j;` `            `  `            ``for` `(j = ``0``; j < m; j++)` `                ``if` `(a[i] == b[j])` `                    ``break``;`   `            ``if` `(j == m)` `                ``System.out.print(a[i] + ``" "``);` `        ``}` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `a[] = { ``1``, ``2``, ``6``, ``3``, ``4``, ``5` `};` `        ``int` `b[] = { ``2``, ``4``, ``3``, ``1``, ``0` `};` `        `  `        ``int` `n = a.length;` `        ``int` `m = b.length;` `        `  `        ``findMissing(a, b, n, m);` `    ``}` `}`   `// This code is contributed` `// by Anant Agarwal.`

## Python 3

 `# Python 3 simple program to find elements ` `# which are not present in second array`   `# Function for finding elements which ` `# are there in a[] but not in b[].` `def` `findMissing(a, b, n, m):`   `    ``for` `i ``in` `range``(n):` `        ``for` `j ``in` `range``(m):` `            ``if` `(a[i] ``=``=` `b[j]):` `                ``break`   `        ``if` `(j ``=``=` `m ``-` `1``):` `            ``print``(a[i], end ``=` `" "``)`   `# Driver code` `if` `__name__ ``=``=` `"__main__"``:` `    `  `    ``a ``=` `[ ``1``, ``2``, ``6``, ``3``, ``4``, ``5` `]` `    ``b ``=` `[ ``2``, ``4``, ``3``, ``1``, ``0` `]` `    ``n ``=` `len``(a)` `    ``m ``=` `len``(b)` `    ``findMissing(a, b, n, m)`   `# This code is contributed ` `# by ChitraNayal`

## C#

 `// C# simple program to find elements` `// which are not present in second array` `using` `System;`   `class` `GFG {` `    `  `    ``// Function for finding elements ` `    ``// which are there in a[] but not` `    ``// in b[].` `    ``static` `void` `findMissing(``int` `[]a, ``int` `[]b, ` `                            ``int` `n, ``int` `m)` `    ``{` `        ``for` `(``int` `i = 0; i < n; i++)` `        ``{` `            ``int` `j;` `            `  `            ``for` `(j = 0; j < m; j++)` `                ``if` `(a[i] == b[j])` `                    ``break``;`   `            ``if` `(j == m)` `                ``Console.Write(a[i] + ``" "``);` `        ``}` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `[]a = {1, 2, 6, 3, 4, 5};` `        ``int` `[]b = {2, 4, 3, 1, 0};` `        `  `        ``int` `n = a.Length;` `        ``int` `m = b.Length;` `        `  `        ``findMissing(a, b, n, m);` `    ``}` `}`   `// This code is contributed by vt_m.`

## PHP

 ` `

## Javascript

 ``

Output

`6 5 `

Time complexity: O(n*m) since using inner and outer loops
Auxiliary Space : O(1)

Method 2 (Use Hashing): In this method, we store all elements of second array in a hash table (unordered_set). One by one check all elements of first array and print all those elements which are not present in the hash table.

Implementation:

## C++

 `// C++ efficient program to` `// find elements which are not` `// present in second array` `#include` `using` `namespace` `std;`   `// Function for finding` `// elements which are there` `// in a[] but not in b[].` `void` `findMissing(``int` `a[], ``int` `b[],` `                ``int` `n, ``int` `m)` `{` `    ``// Store all elements of` `    ``// second array in a hash table` `    ``unordered_set <``int``> s;` `    ``for` `(``int` `i = 0; i < m; i++)` `        ``s.insert(b[i]);`   `    ``// Print all elements of` `    ``// first array that are not` `    ``// present in hash table` `    ``for` `(``int` `i = 0; i < n; i++)` `        ``if` `(s.find(a[i]) == s.end())` `            ``cout << a[i] << ``" "``;` `}`   `// 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]);` `    ``int` `m = ``sizeof``(b) / ``sizeof``(b[1]);` `    ``findMissing(a, b, n, m);` `    ``return` `0;` `}`

## Java

 `// Java efficient program to find elements` `// which are not present in second array` `import` `java.util.HashSet;` `import` `java.util.Set;`   `public` `class` `GfG{`   `    ``// Function for finding elements which` `    ``// are there in a[] but not in b[].` `    ``static` `void` `findMissing(``int` `a[], ``int` `b[],` `                    ``int` `n, ``int` `m)` `    ``{` `        ``// Store all elements of` `        ``// second array in a hash table` `        ``HashSet s = ``new` `HashSet<>();` `        ``for` `(``int` `i = ``0``; i < m; i++)` `            ``s.add(b[i]);` `        `  `        ``// Print all elements of first array` `        ``// that are not present in hash table` `        ``for` `(``int` `i = ``0``; i < n; i++)` `            ``if` `(!s.contains(a[i]))` `                ``System.out.print(a[i] + ``" "``);` `    ``}`   `    ``public` `static` `void` `main(String []args){` `        `  `        ``int` `a[] = { ``1``, ``2``, ``6``, ``3``, ``4``, ``5` `};` `        ``int` `b[] = { ``2``, ``4``, ``3``, ``1``, ``0` `};` `        ``int` `n = a.length;` `        ``int` `m = b.length;` `        ``findMissing(a, b, n, m);` `    ``}` `}` `    `  `// This code is contributed by Rituraj Jain`

## Python3

 `# Python3 efficient program to find elements ` `# which are not present in second array`   `# Function for finding elements which ` `# are there in a[] but not in b[].` `def` `findMissing(a, b, n, m):` `    `  `    ``# Store all elements of second ` `    ``# array in a hash table` `    ``s ``=` `dict``()` `    ``for` `i ``in` `range``(m):` `        ``s[b[i]] ``=` `1`   `    ``# Print all elements of first array ` `    ``# that are not present in hash table` `    ``for` `i ``in` `range``(n):` `        ``if` `a[i] ``not` `in` `s.keys():` `            ``print``(a[i], end ``=` `" "``)`   `# Driver code` `a ``=` `[ ``1``, ``2``, ``6``, ``3``, ``4``, ``5` `]` `b ``=` `[ ``2``, ``4``, ``3``, ``1``, ``0` `]` `n ``=` `len``(a)` `m ``=` `len``(b)` `findMissing(a, b, n, m)`   `# This code is contributed by mohit kumar`

## C#

 `// C# efficient program to find elements ` `// which are not present in second array ` `using` `System; ` `using` `System.Collections.Generic;`   `class` `GfG` `{`   `    ``// Function for finding elements which ` `    ``// are there in a[] but not in b[]. ` `    ``static` `void` `findMissing(``int` `[]a, ``int` `[]b, ` `                    ``int` `n, ``int` `m) ` `    ``{ ` `        ``// Store all elements of ` `        ``// second array in a hash table ` `        ``HashSet<``int``> s = ``new` `HashSet<``int``>(); ` `        ``for` `(``int` `i = 0; i < m; i++) ` `            ``s.Add(b[i]); ` `        `  `        ``// Print all elements of first array ` `        ``// that are not present in hash table ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``if` `(!s.Contains(a[i]))` `                ``Console.Write(a[i] + ``" "``); ` `    ``} `   `    ``// Driver code` `    ``public` `static` `void` `Main(String []args)` `    ``{` `        ``int` `[]a = { 1, 2, 6, 3, 4, 5 }; ` `        ``int` `[]b = { 2, 4, 3, 1, 0 }; ` `        ``int` `n = a.Length; ` `        ``int` `m = b.Length; ` `        ``findMissing(a, b, n, m);` `    ``}` `}`   `/* This code contributed by PrinciRaj1992 */`

## Javascript

 ``

Output

`6 5 `

Time complexity : O(n+m)
Auxiliary Space : O(n)

This article is contributed by DANISH_RAZA . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

## Approach 3: Recursion

### Algorithm:

1. “findMissing” function takes four parameters, array “a” of size “n” and array “b” of size “m”.
2. Base case : If n==0 ,  then there are no more elements left to check, so return from the function.
3. Recursive case : Check if the first element of array “a” is present in array “b”.  For this, use a for loop and iterate over all elements of array “b”.
4. If first element of array “a” is not in array “b”, print it.
5. Recursively call the “findMissing” function with the remaining elements of array “a” and array “b”. For this, increment the pointer of array “a” and decrease it’s size “n” by 1.
6. Call the recursive function “findMissing” in main() with array “a” and array “b”, and their respective sizes “n” and “m”.

Here’s the implementation:

## C++

 `#include ` `using` `namespace` `std;`   `void` `findMissing(``int` `a[], ``int` `b[], ``int` `n, ``int` `m) {` `    ``// Base case: if n is zero, then there are no more elements to check` `    ``if` `(n == 0) {` `        ``return``;` `    ``}` `    `  `    ``// Recursive case: check if the first element of a[] is in b[]` `    ``int` `i;` `    ``for` `(i = 0; i < m; i++) {` `        ``if` `(a[0] == b[i]) {` `            ``break``;` `        ``}` `    ``}` `    `  `    ``// If the first element of a[] is not in b[], print it` `    ``if` `(i == m) {` `        ``cout << a[0] << ``" "``;` `    ``}` `    `  `    ``// Recursively call findMissing with the remaining elements of a[] and b[]` `    ``findMissing(a+1, b, n-1, m);` `}`   `int` `main() {` `    ``int` `a[] = { 1, 2, 6, 3, 4, 5 };` `    ``int` `b[] = { 2, 4, 3, 1, 0 };` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]);` `    ``int` `m = ``sizeof``(b) / ``sizeof``(b[1]);` `    `  `   `  `    ``findMissing(a, b, n, m);` `    ``cout << endl;` `    `  `    ``return` `0;` `}`   `// This code is contributed by Vaibhav Saroj`

## C

 `#include `   `void` `findMissing(``int` `a[], ``int` `b[], ``int` `n, ``int` `m) {` `    ``// Base case: if n is zero, then there are no more elements to check` `    ``if` `(n == 0) {` `        ``return``;` `    ``}` `    `  `    ``// Recursive case: check if the first element of a[] is in b[]` `    ``int` `i;` `    ``for` `(i = 0; i < m; i++) {` `        ``if` `(a[0] == b[i]) {` `            ``break``;` `        ``}` `    ``}` `    `  `    ``// If the first element of a[] is not in b[], print it` `    ``if` `(i == m) {` `        ``printf``(``"%d "``, a[0]);` `    ``}` `    `  `    ``// Recursively call findMissing with the remaining elements of a[] and b[]` `    ``findMissing(a+1, b, n-1, m);` `}`   `int` `main() {` `    ``int` `a[] = { 1, 2, 6, 3, 4, 5 };` `    ``int` `b[] = { 2, 4, 3, 1, 0 };` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]);` `    ``int` `m = ``sizeof``(b) / ``sizeof``(b[0]);` `    `  `    ``findMissing(a, b, n, m);` `    ``printf``(``"\n"``);` `    `  `    ``return` `0;` `}`       `// This code is contributed by Vaibhav Saroj`

## Java

 `import` `java.util.*;`   `class` `Main {` `    ``public` `static` `void` `findMissing(``int``[] a, ``int``[] b, ``int` `n, ``int` `m) {` `        ``// Base case: if n is zero, then there are no more elements to check` `        ``if` `(n == ``0``) {` `            ``return``;` `        ``}`   `        ``// Recursive case: check if the first element of a[] is in b[]` `        ``int` `i;` `        ``for` `(i = ``0``; i < m; i++) {` `            ``if` `(a[``0``] == b[i]) {` `                ``break``;` `            ``}` `        ``}`   `        ``// If the first element of a[] is not in b[], print it` `        ``if` `(i == m) {` `            ``System.out.print(a[``0``] + ``" "``);` `        ``}`   `        ``// Recursively call findMissing with the remaining elements of a[] and b[]` `        ``findMissing(Arrays.copyOfRange(a, ``1``, n), b, n-``1``, m);` `    ``}`   `    ``public` `static` `void` `main(String[] args) {` `        ``int``[] a = { ``1``, ``2``, ``6``, ``3``, ``4``, ``5` `};` `        ``int``[] b = { ``2``, ``4``, ``3``, ``1``, ``0` `};` `        ``int` `n = a.length;` `        ``int` `m = b.length;`   `        ``findMissing(a, b, n, m);` `        ``System.out.println();` `    ``}` `}`       `// This code is contributed by Vaibhav Saroj`

## Javascript

 `function` `findMissing(a, b, n, m) {` `  ``// Base case: if n is zero, then there are no more elements to check` `  ``if` `(n === 0) {` `    ``return``;` `  ``}`   `  ``// Recursive case: check if the first element of a[] is in b[]` `  ``let i;` `  ``for` `(i = 0; i < m; i++) {` `    ``if` `(a[0] === b[i]) {` `      ``break``;` `    ``}` `  ``}`   `  ``// If the first element of a[] is not in b[], print it` `  ``if` `(i === m) {` `    ``console.log(a[0] + ``" "``);` `  ``}`   `  ``// Recursively call findMissing with the remaining elements of a[] and b[]` `  ``findMissing(a.slice(1), b, n - 1, m);` `}`   `const a = [1, 2, 6, 3, 4, 5];` `const b = [2, 4, 3, 1, 0];` `const n = a.length;` `const m = b.length;`   `findMissing(a, b, n, m);` `console.log(); ``// add a newline at the end`   `// This code is contributed by Vaibhav Saroj`

Output

```6 5
```

This Recursive approach and code is contributed by Vaibhav Saroj .

The time and space complexity:

Time complexity :  O(nm) .

Space complexity :  O(1) .

My Personal Notes arrow_drop_up