Let A and B be two nonsorted arrays Assume that all the numb
Let A and B be two non-sorted arrays. Assume that all the numbers in A are pairwise disjoint and the same is true for B. Write an efficient as you can algorithm that lists all the elements (values) of B that that do not appear in A. Example: A = [4, 2, 5, 3, 7, 1], B = [13, 7, 4, 21, 5, 3]. The values of B that do not appear in A are 13, 21.
Ps no code please, thank you
Solution
Hi, WE can solve using Hash Set.
Steps:
1. Add all elements of A in HashSet.
2. Iterate through elements of B:
2.1 IF current element of B is not in HashSet, then print this number
3. END
Time Complexity: O(N+M) : N: number of elements of A, M : Number of elements in B
Sapce Complexity: O(N)
