Find elements which are present in first array and not in second - GeeksforGeeks

11 Aug, 2021
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. 
C++
Java
Python 3
C#
PHP
Javascript
// C++ simple program to// find elements which are// not present in second array#include<bits/stdc++.h>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 codeint 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 simple program to// find elements which are// not present in second arrayclass 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 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 codeif __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# simple program to find elements// which are not present in second arrayusing 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// PHP 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[].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] , " ";    }} // Driver code$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); // This code is contributed by anuj_67.?>
<script> // Javascript 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[].    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] + " ");        }    }         // Driver Code    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);              // This code is contributed by avanitrachhadiya2155     </script>
Output : 
6 5
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. 
C++
Java
Python3
C#
Javascript
// C++ efficient program to// find elements which are not// present in second array#include<bits/stdc++.h>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 codeint 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 efficient program to find elements// which are not present in second arrayimport 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<Integer> 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 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 codea = [ 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# efficient program to find elements// which are not present in second arrayusing 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 */
<script>// Javascript 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[].    function findMissing(a,b,n,m)    {        // Store all elements of        // second array in a hash table        let s = new Set();        for (let i = 0; i < m; i++)            s.add(b[i]);                  // Print all elements of first array        // that are not present in hash table        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);      // This code is contributed by patel2127</script>
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.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
ukasp
mohit kumar 29
rituraj_jain
princiraj1992
avanitrachhadiya2155
patel2127
mohammedsadathkhan123
Accolite
Snapdeal
Zoho
Arrays
Hash
Zoho
Accolite
Snapdeal
Arrays
Hash
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Hashing | Set 1 (Introduction)
Hashing | Set 3 (Open Addressing)
Count pairs with given sum
Hashing | Set 2 (Separate Chaining)