aboutsummaryrefslogtreecommitdiff
path: root/diff_two_array.cpp
blob: 65bbd2afba9e7b73e82f21e9a25bcbcc763dc229 (plain)
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
// 2215. Find the Difference of Two Arrays
#include "leetcode.h"

/*
 * given two zero-indexed arrays 'n1' and 'n2', return a list
 * 'ans' of size 2 where:
 * 'ans[0]' is a list of all distinct integers in 'n1' which
 * are not present in 'n2'
 * 'ans[1]' is a list of all distinct integers in 'n2' which
 * are not present in 'n1'
 * note that the integers in the lists may be returned in any order
 */

class Solution {
public:
  vvd(int) findDifference(vector<int> &n1, vector<int> &n2) {
    unordered_set<int> us1(begin(n1), end(n1)), us2(begin(n2), end(n2));
    vvd(int) ans(2);
    for (int i : us1)
      if (!us2.count(i))
        ans[0].push_back(i);
    for (int i : us2)
      if (!us1.count(i))
        ans[1].push_back(i);
    return ans;
  }
};

int main() {
  Solution obj;
  vector<int> n11 = {1, 2, 3}, n21 = {2, 4, 6};
  vector<int> n12 = {1, 2, 3, 3}, n22 = {1, 1, 2, 2};
  vvd(int) fd1 = obj.findDifference(n11, n21);
  vvd(int) fd2 = obj.findDifference(n12, n22);
}