-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find similar element.txt
62 lines (37 loc) · 1.4 KB
/
Find similar element.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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.
class Solution{
public:
vector<long long> findMissing(long long a[], long long b[],
int n, int m)
{
// Store all elements of
// second array in a hash table
unordered_set <long long> s;
vector<long long> ans;
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())
ans.push_back(a[i]);
return ans;
}
};