Skip to content

Commit

Permalink
Merge pull request #602 from Spidy-Coder/Spidy-Coder-patch-1
Browse files Browse the repository at this point in the history
Adding Union of two arrays.cpp
  • Loading branch information
SR-Sunny-Raj committed Oct 3, 2021
2 parents 328d665 + 3508978 commit 3373e6e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 08. Hashing/Union of two arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//Problem statement URL:- https://practice.geeksforgeeks.org/problems/union-of-two-arrays3538/1/?track=DSASP-Hashing&batchId=154
//Hashing approach

class Solution{
public:
//Function to return the count of number of elements in union of two arrays.
int doUnion(int a[], int n, int b[], int m) {
// Efficient Approach:-step-wise

// 1. Create a set, and insert a[] elements which will be distinct in set
unordered_set<int> s(a,a+n);

// 2. Insert b[] elements in the same set
for(int i=0;i<m;i++){
s.insert(b[i]); //disinct elements will only be inserted.
}

// 3. size of our created set s
return s.size();
}
};

0 comments on commit 3373e6e

Please sign in to comment.