Given two integer arrays a[] and b[], find the elements which are present in the first array a[], but not present in the second array b[]. Return the elements in the same order in which they appear in a[].
Examples:Â
Input: a[] = [1, 2, 3, 4, 5, 10], b[] = [2, 3, 1, 0, 5]
Output: [4, 10]
Explanation: 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]
Explanation: Second array does not contain element 5.Input: a[] = [9], b[] = [7, 9, 4, 9, 9, 9]
Output: []
Table of Content
[Naive Approach] Using Linear Search - O(n * m) Time and O(1) Space
The idea is to check every element of a[] in b[]. If an element of a[] is not found in b[], add it to the result.
Working of Approach:
- Traverse each element of a[].
- For every element, search for it in b[].
- If the element is not found in b[], add it to ans.
- Continue this for all elements of a[].
- The result automatically maintains the order of a[].
#include <iostream>
#include <vector>
using namespace std;
vector<int> findMissing(vector<int> &a, vector<int> &b)
{
vector<int> res;
// Traverse all elements of the first array.
for (int i = 0; i < a.size(); i++)
{
bool found = false;
// Search for the current element in the second array.
for (int j = 0; j < b.size(); j++)
{
if (a[i] == b[j])
{
found = true;
break;
}
}
// If the element is not present in b[], add it to the result.
if (!found)
res.push_back(a[i]);
}
return res;
}
int main()
{
vector<int> a = {1, 2, 3, 4, 5, 10};
vector<int> b = {2, 3, 1, 0, 5};
vector<int> ans = findMissing(a, b);
cout << "[";
for (int i = 0; i < ans.size(); i++)
{
if (i > 0)
cout << ", ";
cout << ans[i];
}
cout << "]";
return 0;
}
import java.util.*;
class GFG {
static ArrayList<Integer> findMissing(int[] a, int[] b)
{
ArrayList<Integer> res = new ArrayList<>();
// Traverse all elements of the first array.
for (int i = 0; i < a.length; i++) {
boolean found = false;
// Search for the current element in the second
// array.
for (int j = 0; j < b.length; j++) {
if (a[i] == b[j]) {
found = true;
break;
}
}
// If the element is not present in b[], add it
// to the result.
if (!found)
res.add(a[i]);
}
return res;
}
public static void main(String[] args)
{
int[] a = { 1, 2, 3, 4, 5, 10 };
int[] b = { 2, 3, 1, 0, 5 };
ArrayList<Integer> ans = findMissing(a, b);
// Print the result.
System.out.println(ans);
}
}
def findMissing(a, b):
res = []
# Traverse all elements of the first array.
for i in range(len(a)):
found = False
# Search for the current element in the second array.
for j in range(len(b)):
if a[i] == b[j]:
found = True
break
# If the element is not present in b[], add it to the result.
if not found:
res.append(a[i])
return res
if __name__ == "__main__":
a = [1, 2, 3, 4, 5, 10]
b = [2, 3, 1, 0, 5]
ans = findMissing(a, b)
# Print the result.
print(ans)
using System;
using System.Collections.Generic;
class GFG {
static List<int> findMissing(int[] a, int[] b)
{
List<int> res = new List<int>();
// Traverse all elements of the first array.
for (int i = 0; i < a.Length; i++) {
bool found = false;
// Search for the current element in the second
// array.
for (int j = 0; j < b.Length; j++) {
if (a[i] == b[j]) {
found = true;
break;
}
}
// If the element is not present in b[], add it
// to the result.
if (!found)
res.Add(a[i]);
}
return res;
}
static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 10 };
int[] b = { 2, 3, 1, 0, 5 };
List<int> ans = findMissing(a, b);
// Print the result.
Console.WriteLine("[" + string.Join(", ", ans)
+ "]");
}
}
function findMissing(a, b)
{
let res = [];
// Traverse all elements of the first array.
for (let i = 0; i < a.length; i++) {
let found = false;
// Search for the current element in the second
// array.
for (let j = 0; j < b.length; j++) {
if (a[i] === b[j]) {
found = true;
break;
}
}
// If the element is not present in b[], add it to
// the result.
if (!found)
res.push(a[i]);
}
return res;
}
// Driver Code
let a = [ 1, 2, 3, 4, 5, 10 ];
let b = [ 2, 3, 1, 0, 5 ];
let ans = findMissing(a, b);
console.log("[");
for (let i = 0; i < ans.length; i++) {
if (i > 0)
console.log(", ");
console.log(ans[i]);
}
console.log("]");
Output
[4, 10]
[Expected Approach] Using Hashing - O(n + m) Time and O(m) Space
The idea is to store all elements of b[] in a hash set. Then, traverse a[] and add only those elements which are not present in the set.
Working of Approach:
- Create an unordered_set to store elements of b[].
- Insert every element of b[] into the set.
- Traverse a[] from left to right.
- Check whether each element exists in the hash set.
- If it does not exist, add it to res.
Let us understand with an example:
Input: a[] = [1, 2, 3, 4, 5, 10], b[] = [2, 3, 1, 0, 5]
- Store all elements of b[] = [2, 3, 1, 0, 5] in the hash set: {0, 1, 2, 3, 5}.
- Traverse a[]: 1, 2, 3 are found in the set, so they are skipped.
- 4 is not present in the set, so add it to res: [4].
- 5 is found in the set, so skip it; 10 is not found, so add it.
- Final result is [4, 10].
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
vector<int> findMissing(vector<int> &a, vector<int> &b)
{
int n = a.size(), m = b.size();
// Store all elements of
// second array in a hash table
unordered_set<int> s;
vector<int> res;
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())
res.push_back(a[i]);
return res;
}
int main()
{
vector<int> a = {1, 2, 3, 4, 5, 10};
vector<int> b = {2, 3, 1, 0, 5};
vector<int> ans = findMissing(a, b);
cout << "[";
for (int i = 0; i < ans.size(); i++)
{
if (i > 0)
cout << ", ";
cout << ans[i];
}
cout << "]";
return 0;
}
import java.util.*;
class GFG {
static ArrayList<Integer> findMissing(int[] a, int[] b)
{
int n = a.length, m = b.length;
// Store all elements of
// second array in a hash table
HashSet<Integer> s = new HashSet<>();
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < m; i++)
s.add(b[i]);
// Add all elements of
// first array that are not
// present in hash table
for (int i = 0; i < n; i++)
if (!s.contains(a[i]))
res.add(a[i]);
return res;
}
public static void main(String[] args)
{
int[] a = { 1, 2, 3, 4, 5, 10 };
int[] b = { 2, 3, 1, 0, 5 };
ArrayList<Integer> ans = findMissing(a, b);
// Print the result.
System.out.println(ans);
}
}
def findMissing(a, b):
n = len(a)
m = len(b)
# Store all elements of
# second array in a hash table
s = set()
res = []
for i in range(m):
s.add(b[i])
# Print all elements of
# first array that are not
# present in hash table
for i in range(n):
if a[i] not in s:
res.append(a[i])
return res
if __name__ == "__main__":
a = [1, 2, 3, 4, 5, 10]
b = [2, 3, 1, 0, 5]
ans = findMissing(a, b)
print(ans)
using System;
using System.Collections.Generic;
class GFG {
static List<int> findMissing(int[] a, int[] b)
{
int n = a.Length, m = b.Length;
// Store all elements of
// second array in a hash table
HashSet<int> s = new HashSet<int>();
List<int> res = new List<int>();
for (int i = 0; i < m; i++)
s.Add(b[i]);
// Add all elements of
// first array that are not
// present in hash table
for (int i = 0; i < n; i++)
if (!s.Contains(a[i]))
res.Add(a[i]);
return res;
}
static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 10 };
int[] b = { 2, 3, 1, 0, 5 };
List<int> ans = findMissing(a, b);
// Print the result.
Console.WriteLine("[" + string.Join(", ", ans)
+ "]");
}
}
function findMissing(a, b)
{
let n = a.length, m = b.length;
// Store all elements of
// second array in a hash table
let s = new Set();
let res = [];
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]))
res.push(a[i]);
return res;
}
// Driver Code
let a = [ 1, 2, 3, 4, 5, 10 ];
let b = [ 2, 3, 1, 0, 5 ];
let ans = findMissing(a, b);
console.log("[");
for (let i = 0; i < ans.length; i++) {
if (i > 0)
console.log(", ");
console.log(ans[i]);
}
console.log("]");
Output
[4, 10]