Find elements which are present in first array and not in second
Last Updated :
20 Sep, 2023
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
Method 1 (Simple): A Naive Approach is to use two loops and check element which not present in second array.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
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] << " " ;
}
}
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
class GFG
{
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] + " " );
}
}
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);
}
}
|
Python 3
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 = " " )
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)
|
C#
using System;
class GFG {
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] + " " );
}
}
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);
}
}
|
Javascript
<script>
function findMissing(a,b,n,m)
{
for (let i = 0; i < n; i++)
{
let j;
for (j = 0; j < m; j++)
if (a[i] == b[j])
break ;
if (j == m)
document.write(a[i] + " " );
}
}
let a=[ 1, 2, 6, 3, 4, 5 ];
let b=[2, 4, 3, 1, 0];
let n = a.length;
let m = b.length;
findMissing(a, b, n, m);
</script>
|
PHP
<?php
function findMissing( $a , $b , $n , $m )
{
for ( $i = 0; $i < $n ; $i ++)
{
$j ;
for ( $j = 0; $j < $m ; $j ++)
if ( $a [ $i ] == $b [ $j ])
break ;
if ( $j == $m )
echo $a [ $i ] , " " ;
}
}
$a = array ( 1, 2, 6, 3, 4, 5 );
$b = array ( 2, 4, 3, 1, 0 );
$n = count ( $a );
$m = count ( $b );
findMissing( $a , $b , $n , $m );
?>
|
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++
#include<bits/stdc++.h>
using namespace std;
void findMissing( int a[], int b[],
int n, int m)
{
unordered_set < int > s;
for ( int i = 0; i < m; i++)
s.insert(b[i]);
for ( int i = 0; i < n; i++)
if (s.find(a[i]) == s.end())
cout << a[i] << " " ;
}
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
import java.util.HashSet;
import java.util.Set;
public class GfG{
static void findMissing( int a[], int b[],
int n, int m)
{
HashSet<Integer> s = new HashSet<>();
for ( int i = 0 ; i < m; i++)
s.add(b[i]);
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);
}
}
|
Python3
def findMissing(a, b, n, m):
s = dict ()
for i in range (m):
s[b[i]] = 1
for i in range (n):
if a[i] not in s.keys():
print (a[i], end = " " )
a = [ 1 , 2 , 6 , 3 , 4 , 5 ]
b = [ 2 , 4 , 3 , 1 , 0 ]
n = len (a)
m = len (b)
findMissing(a, b, n, m)
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static void findMissing( int []a, int []b,
int n, int m)
{
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < m; i++)
s.Add(b[i]);
for ( int i = 0; i < n; i++)
if (!s.Contains(a[i]))
Console.Write(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);
}
}
|
Javascript
<script>
function findMissing(a,b,n,m)
{
let s = new Set();
for (let i = 0; i < m; i++)
s.add(b[i]);
for (let i = 0; i < n; i++)
if (!s.has(a[i]))
document.write(a[i] + " " );
}
let a=[1, 2, 6, 3, 4, 5 ];
let b=[2, 4, 3, 1, 0];
let n = a.length;
let m = b.length;
findMissing(a, b, n, m);
</script>
|
Time complexity : O(n+m)
Auxiliary Space : O(n)
Approach 3: Recursion
Algorithm:
- “findMissing” function takes four parameters, array “a” of size “n” and array “b” of size “m”.
- Base case : If n==0 , then there are no more elements left to check, so return from the function.
- 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”.
- If first element of array “a” is not in array “b”, print it.
- 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.
- 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 <iostream>
using namespace std;
void findMissing( int a[], int b[], int n, int m) {
if (n == 0) {
return ;
}
int i;
for (i = 0; i < m; i++) {
if (a[0] == b[i]) {
break ;
}
}
if (i == m) {
cout << a[0] << " " ;
}
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;
}
|
C
#include <stdio.h>
void findMissing( int a[], int b[], int n, int m) {
if (n == 0) {
return ;
}
int i;
for (i = 0; i < m; i++) {
if (a[0] == b[i]) {
break ;
}
}
if (i == m) {
printf ( "%d " , a[0]);
}
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;
}
|
Java
import java.util.*;
class Main {
public static void findMissing( int [] a, int [] b, int n, int m) {
if (n == 0 ) {
return ;
}
int i;
for (i = 0 ; i < m; i++) {
if (a[ 0 ] == b[i]) {
break ;
}
}
if (i == m) {
System.out.print(a[ 0 ] + " " );
}
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();
}
}
|
Python3
def find_missing(a, b, n, m):
if n = = 0 :
return
i = 0
while i < m:
if a[ 0 ] = = b[i]:
break
i + = 1
if i = = m:
print (a[ 0 ], end = " " )
find_missing(a[ 1 :], b, n - 1 , m)
def main():
a = [ 1 , 2 , 6 , 3 , 4 , 5 ]
b = [ 2 , 4 , 3 , 1 , 0 ]
n = len (a)
m = len (b)
find_missing(a, b, n, m)
print ()
if __name__ = = "__main__" :
main()
|
C#
using System;
public class Program
{
public static void FindMissing( int [] a, int [] b, int n, int m)
{
if (n == 0)
{
return ;
}
int i;
for (i = 0; i < m; i++)
{
if (a[0] == b[i])
{
break ;
}
}
if (i == m)
{
Console.Write(a[0] + " " );
}
FindMissing( new ArraySegment< int >(a, 1, n - 1).ToArray(), 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);
Console.WriteLine();
}
}
|
Javascript
function findMissing(a, b, n, m) {
if (n === 0) {
return ;
}
let i;
for (i = 0; i < m; i++) {
if (a[0] === b[i]) {
break ;
}
}
if (i === m) {
console.log(a[0] + " " );
}
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();
|
This Recursive approach and code is contributed by Vaibhav Saroj .
The time and space complexity:
Time complexity : O(nm) .
Space complexity : O(1) .
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...