Given two arrays A and B contains integers of size N and M, the task is to find numbers which are present in the first array, but not present in the second array.
Example 1:
Input: N = 6, M = 5
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.
Example 2:
Input: N = 5, M = 5
A[] = {4, 3, 5, 9, 11}
B[] = {4, 9, 3, 11, 10}
Output: 5  
Explanation: Second array does not 
contain element 5.
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function findMissing() that takes array A, array B, integer N, and integer M  as parameters and returns an array that contains the missing elements and the order of the elements should be the same as they are in array A.
 
Expected Time Complexity: O(N+M).
Expected Auxiliary Space: O(M).
 
Constraints:
1 ≤ N, M ≤ 10^{6}
-10^{6} ≤ A[i], B[i] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Starter code: #User function Template for python3







def findMissing(A,B,N,M):

    # code here



Solutions:
def findMissing(A, B, N, M):
	d = dict()
	result = []
	for i in B:
		d[i] = 1
	for i in A:
		if i not in d:
			result.append(i)
	return result


def findMissing(A, B, N, M):
	b = set(B)
	l = []
	for i in A:
		if i not in b:
			l.append(i)
	return l


def findMissing(A, B, N, M):
	l = []
	a = set(A)
	b = set(B)
	c = a.intersection(b)
	for i in A:
		if i not in c:
			l.append(i)
	return l


def findMissing(A, B, N, M):
	g = set()
	for i in range(0, M):
		g.add(B[i])
	ans = []
	for i in range(0, N):
		if A[i] not in g:
			ans.append(A[i])
	return ans


def findMissing(A, B, N, M):
	mp = {}
	for i in range(M):
		mp[B[i]] = 1
	ans = []
	for i in range(N):
		if not mp.get(A[i]):
			ans.append(A[i])
	return ans


def findMissing(A, B, N, M):
	(a, b) = ({}, {})
	for j in B:
		b[j] = b.get(j, 0) + 1
	ans = []
	for i in A:
		if b.get(i, -1) == -1:
			ans += [i]
	return ans


def findMissing(A, B, N, M):
	setB = set(B)
	missing = []
	for num in A:
		if num not in setB:
			missing.append(num)
	return missing


def findMissing(A, B, N, M):
	arr = []
	s = set()
	for ele in B:
		s.add(ele)
	for i in range(N):
		if A[i] not in s:
			arr.append(A[i])
			continue
	return arr


def findMissing(A, B, N, M):
	se = set(tuple(B))
	ans = []
	for i in A:
		if i not in se:
			ans.append(i)
	return ans


def findMissing(A, B, N, M):
	set_B = set(B)
	missing_elements = []
	for elem in A:
		if elem not in set_B:
			missing_elements.append(elem)
	return missing_elements


def findMissing(A, B, N, M):
	s = set(tuple(B))
	a = []
	for i in A:
		if i not in s:
			a.append(i)
	return a


def findMissing(A, B, N, M):
	from collections import defaultdict
	dic = defaultdict(int)
	for i in B:
		dic[i] += 1
	l = []
	for i in A:
		if dic[i] == 0:
			l.append(i)
	return l


def findMissing(A, B, N, M):
	hash = {}
	ans = []
	for i in B:
		if i in hash:
			hash[i] += 1
		else:
			hash[i] = 1
	for i in A:
		if i not in hash:
			ans.append(i)
	return ans


def findMissing(A, B, N, M):
	s = set(B)
	ans = []
	for i in A:
		if i not in s:
			ans.append(i)
	return ans


def findMissing(A, B, N, M):
	D = set(B)
	res = []
	for i in A:
		if i in D:
			continue
		else:
			res.append(i)
	return res


def findMissing(A, B, N, M):
	ans = []
	d = {}
	for i in range(M):
		d[B[i]] = 0
	for i in range(len(A)):
		if A[i] not in d:
			ans.append(A[i])
	return ans


def findMissing(A, B, N, M):
	s = {}
	ans = []
	for i in range(M):
		s[B[i]] = 1
	for i in range(N):
		if A[i] not in s.keys():
			ans.append(A[i])
	return ans


def findMissing(A, B, N, M):
	B = set(B)
	return [j for j in A if j not in B]


def findMissing(A, B, N, M):
	hash_table = dict()
	arr = []
	for i in B:
		hash_table[i] = 1
	for j in A:
		if j not in hash_table:
			arr.append(j)
	return arr


def findMissing(A, B, N, M):
	K = set(B)
	R = []
	for i in A:
		if i not in K:
			R.append(i)
	return R


def findMissing(A, B, N, M):
	d = set(B)
	c = []
	for i in A:
		if i not in d:
			c.append(i)
	return c


def findMissing(A, B, N, M):
	B_set = set(B)
	result = []
	for num in A:
		if num not in B_set:
			result.append(num)
	return result


def findMissing(A, B, N, M):
	f = {}
	for i in B:
		f[i] = f.get(i, 0)
	ans = []
	for i in A:
		if i not in f:
			ans.append(i)
	return ans


def findMissing(A, B, N, M):
	setB = set(B)
	missing = []
	for i in range(N):
		if A[i] not in setB:
			missing.append(A[i])
	return missing


def findMissing(A, B, N, M):
	arr = []
	freq = {}
	for i in B:
		freq[i] = freq.get(i, 0) + 1
	for i in A:
		freq[i] = freq.get(i, 0) + 1
		if freq.get(i) <= 1:
			arr.append(i)
			freq.pop(i)
	return arr


def findMissing(A, B, N, M):
	l = []
	d = dict()
	for i in B:
		d[i] = 1
	for j in A:
		if j not in d:
			l.append(j)
	return l


from collections import defaultdict as dd

def findMissing(A, B, N, M):
	d1 = dd(int)
	d2 = dd(int)
	for i in A:
		d1[i] += 1
	for j in B:
		d2[j] += 1
	w = []
	for i in A:
		if i not in d2:
			w.append(i)
	return w


def findMissing(A, B, N, M):
	res = []
	B = set(B)
	for i in A:
		if i not in B:
			res.append(i)
	return res


import numpy

def findMissing(A, B, N, M):
	s = set(B)
	arr = []
	for i in A:
		if i not in s:
			arr.append(i)
	return arr


def findMissing(A, B, N, M):
	result = []
	set_b = set(B)
	for a in A:
		if not a in set_b:
			result.append(a)
	return result


def findMissing(A, B, N, M):
	k = []
	s = dict()
	for i in B:
		s[i] = 1
	for i in A:
		if i not in s.keys():
			k.append(i)
	return k


def findMissing(A, B, N, M):
	f = []
	st = set(B)
	for i in A:
		if i not in st:
			f.append(i)
	return f


def findMissing(A, B, N, M):
	ans = []
	dict = {}
	for i in B:
		dict[i] = 1
	for i in A:
		if i not in dict:
			ans.append(i)
	return ans


def findMissing(A, B, N, M):
	a = set(A) - set(B)
	l = [i for i in A if i in a]
	return l


from collections import Counter

def findMissing(A, B, N, M):
	a = set(A) - set(B)
	l = [i for i in A if i in a]
	return l


def findMissing(A, B, N, M):
	v = set()
	res = []
	for each in B:
		v.add(each)
	for each in A:
		if each not in v:
			res.append(each)
	return res


def findMissing(A, B, N, M):
	dic = {}
	for i in A:
		if dic.get(i) == None:
			dic[i] = dic.get(i, 0) + 1
	for i in B:
		if dic.get(i) != None and dic[i] == 1:
			dic[i] = dic.get(i, 0) + 1
	for i in A:
		if dic[i] == 1:
			yield i


def findMissing(A, B, N, M):
	s = set(B)
	return list((a for a in A if not a in s))


def findMissing(A, B, N, M):
	table = {}
	for i in B:
		if i not in table:
			table[i] = 1
	arr = []
	for j in A:
		if j not in table:
			arr += [j]
	return arr


def findMissing(A, B, N, M):
	a = set(B)
	arr = []
	for i in A:
		if i not in a:
			arr.append(i)
	return arr


def findMissing(A, B, N, M):
	list1 = list()
	seta = set(A)
	setb = set(B)
	k = seta.intersection(setb)
	for i in A:
		if i not in k:
			list1.append(i)
	return list1


def findMissing(A, B, N, M):
	l = []
	a = set(A)
	b = set(B)
	c = a.intersection(b)
	C = list(c)
	l = [I for I in A if I not in c]
	return l


def findMissing(a, b, N, M):
	l = []
	b = set(b)
	for i in a:
		if i not in b:
			l.append(i)
	return l


def findMissing(A, B, N, M):
	x = {}
	z = []
	for i in range(M):
		x[B[i]] = 1
	for i in range(N):
		if A[i] not in x.keys():
			z.append(A[i])
	return z


def findMissing(A, B, N, M):
	c = set(A) - set(B)
	b = []
	for i in A:
		if i in c:
			b.append(i)
	return b


def findMissing(A, B, N, M):
	db = {}
	ans = []
	for i in B:
		db[i] = 1
	for i in A:
		if i in db:
			pass
		else:
			ans.append(i)
	return ans


def findMissing(a, b, n, m):
	d = {}
	ans = []
	for i in range(m):
		if b[i] not in d.keys():
			d[b[i]] = 1
		else:
			d[b[i]] += 1
	for i in range(n):
		if a[i] not in d.keys():
			ans.append(a[i])
	return ans


from collections import defaultdict

def findMissing(A, B, N, M):
	d = defaultdict(int)
	for i in range(M):
		d[B[i]] += 1
	res = []
	for i in range(N):
		if A[i] not in d:
			res.append(A[i])
	return res


def findMissing(arr1, arr2, n, m):
	mset = set()
	ans = list()
	for i in arr2:
		mset.add(i)
	for i in arr1:
		if i not in mset:
			ans.append(i)
	return ans


def findMissing(A, B, N, M):
	d = {}
	c = []
	for i in range(M):
		d[B[i]] = 0
	for j in range(N):
		if A[j] not in d:
			c.append(A[j])
	return c


from collections import Counter

def findMissing(a, b, n, m):
	ans = []
	c = Counter(b)
	for i in a:
		if i not in c:
			ans.append(i)
	return ans


def findMissing(A, B, N, M):
	S = set(B)
	lis = []
	for i in A:
		if i not in S:
			lis.append(i)
	return lis


def findMissing(A, B, N, M):
	a = {}
	z = []
	for i in B:
		if i in a:
			a[i] += 1
		else:
			a[i] = 1
	for i in A:
		if i not in a:
			z.append(i)
	return z


def findMissing(A, B, N, M):
	s = set(B)
	lst = list()
	for i in A:
		if i not in s:
			lst.append(i)
	return lst