-
Notifications
You must be signed in to change notification settings - Fork 1
/
Union_of_two_arrays.py
54 lines (46 loc) · 1.51 KB
/
Union_of_two_arrays.py
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
""""
Problem Statement:
-----------------
Given two arrays a[] and b[] of size n and n respectively. The task is to find union between these two arrays.
Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions,
then only one occurrence of element should be printed in union.
Example 1:
--------
Input:
5 3
1 2 3 4 5
1 2 3
Output:
5
Explanation: 1, 2, 3, 4 and 5 are the elements which comes in the union set of both arrays. So count is 5.
Example 2:
---------
Input:
6 2
85 25 1 32 54 6
85 2
Output:
7
Explanation: 85, 25, 1, 32, 54, 6, and 2 are the elements which comes in the union set of both arrays. So count is 7.
Your Task: Complete doUnion funciton that takes a, n, b, m as parameters and returns the count of union elements of the two arrays.The printing is done by the driver code.
Constraints:
1 ≤ n, m ≤ 105
1 ≤ a[i], b[i] < 105
Expected Time Complexity : O((n+m)log(n+m))
Expected Auxilliary Space : O(n+m)
"""
# Link --> https://practice.geeksforgeeks.org/problems/union-of-two-arrays3538/1
# Code:
class Solution:
#Function to return the count of number of elements in union of two arrays.
def doUnion(self,a,n,b,m):
# using a set to contain unique items
temp = a[0]
thisset = {temp}
# adding items from list to the set
thisset.update(b)
thisset.update(a)
count = 0
for i in thisset:
count = count+1
return count