-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #602 from Spidy-Coder/Spidy-Coder-patch-1
Adding Union of two arrays.cpp
- Loading branch information
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |