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) .
Similar Reads
Count elements present in first array but not in second
Given two arrays (which may or may not be sorted) of sizes M and N respectively. Their arrays are such that they might have some common elements in them. You need to count the number of elements whose occurrences are more in the first array than second. Examples: Input : arr1[] = {41, 43, 45, 50}, M
6 min read
Find numbers which are multiples of first array and factors of second array
Given two arrays A[] and B[], the task is to find the integers which are divisible by all the elements of array A[] and divide all the elements of array B[]. Examples: Input: A[] = {1, 2, 2, 4}, B[] = {16, 32, 64} Output: 4 8 16 4, 8 and 16 are the only numbers that are multiples of all the elements
7 min read
Find an array B with at least arr[i] elements in B not equal to the B[i]
Given an array arr[] of size N, the task is to find an array of size N, such that for every ith element of the array arr[], the output array should contain at least arr[i] elements that are not equal to the ith element of the output array. If there exists no such array, then print "Impossible". Exam
9 min read
Count of elements A[i] such that A[i] + 1 is also present in the Array
Given an integer array arr the task is to count the number of elements 'A[i]', such that A[i] + 1 is also present in the array.Note: If there are duplicates in the array, count them separately.Examples: Input: arr = [1, 2, 3] Output: 2 Explanation: 1 and 2 are counted cause 2 and 3 are in arr.Input:
11 min read
Find all Characters in given String which are not present in other String
Given two strings str1 and str2, which are of lengths N and M. The second string contains all the characters of the first string, but there are some extra characters as well. The task is to find all the extra characters present in the second string. Examples: Input: str1 = "abcd", str2 = "dabcdehi"O
10 min read
Check whether K times of a element is present in array
Given an array arr[] and an integer K, the task is to check whether K times of any element are also present in the array. Examples : Input: arr[] = {10, 14, 8, 13, 5}, K = 2 Output: Yes Explanation: K times of 5 is also present in an array, i.e. 10. Input: arr[] = {7, 8, 5, 9, 11}, K = 3 Output: No
8 min read
All elements in an array are Same or not?
Given an array, check whether all elements in an array are the same or not. Examples: Input : "Geeks", "for", "Geeks" Output : Not all Elements are Same Input : 1, 1, 1, 1, 1 Output : All Elements are Same Method 1 (Hashing): We create an empty HashSet, insert all elements into it, then we finally s
5 min read
Find all numbers in range [1, N] that are not present in given Array
Given an array arr[] of size N, where arr[i] is natural numbers less than or equal to N, the task is to find all the numbers in the range [1, N] that are not present in the given array. Examples: Input: arr[ ] = {5, 5, 4, 4, 2}Output: 1 3Explanation: For all numbers in the range [1, 5], 1 and 3 are
4 min read
Find pairs in array whose sum does not exist in Array
Given an array arr[] consisting of N positive integers, the task is to print all pairs of array elements whose sum does not exist in the given array. If no such pair exists, print "-1". Examples: Input: arr[] = {2, 4, 2, 6} Output: (2, 6) (4, 6) (2, 6) Explanation: All possible pairs in the array ar
10 min read
Find just strictly greater element from first array for each element in second array
Given two arrays A[] and B[] containing N elements, the task is to find, for every element in the array B[], the element which is just strictly greater than that element which is present in the array A[]. If no value is present, then print 'null'. Note: The value from the array A[] can only be used
10 min read