|
1 |
| -''' |
2 |
| -A list that has the common distinct element from both arrays and |
3 |
| -if there are repetitions of the element then only one occurrence |
| 1 | +""" |
| 2 | +A list that has the common distinct element from both arrays and |
| 3 | +if there are repetitions of the element then only one occurrence |
4 | 4 | is considered, known as the union of both arrays.
|
5 | 5 |
|
6 |
| -A list that has common distinct elements from both arrays, |
| 6 | +A list that has common distinct elements from both arrays, |
7 | 7 | is the intersection of both arrays.
|
8 | 8 |
|
9 | 9 |
|
10 | 10 | Example:
|
11 | 11 | Input: a[] = {1, 2, 2, 3, 4, 8, 10}, b[] = {4, 6, 3, 1}
|
12 | 12 | Output: {1, 2, 3, 4, 6, 8, 10}
|
13 |
| -Explanation: 1, 2, 3, 4, 6, 8 and 10 is the union of |
| 13 | +Explanation: 1, 2, 3, 4, 6, 8 and 10 is the union of |
14 | 14 | elements present in array a[] and array b[].
|
15 | 15 |
|
16 | 16 | Input: a[] = {1, 2, 2, 3, 4, 8, 10}, b[] = {4, 6, 3, 1}
|
17 | 17 | Output: {1, 2, 3, 4}
|
18 |
| -Explanation: 1, 2, 3, and 4 are the intersection(common elements) |
| 18 | +Explanation: 1, 2, 3, and 4 are the intersection(common elements) |
19 | 19 | of elements present in array a[] and array b[].
|
20 | 20 |
|
21 | 21 | https://www.geeksforgeeks.org/union-and-intersection-of-two-sorted-arrays-2/
|
22 | 22 |
|
23 | 23 |
|
24 |
| -''' |
| 24 | +""" |
25 | 25 |
|
26 |
| -#Taking input lists from user |
| 26 | +# Taking input lists from user |
27 | 27 |
|
28 |
| -a=list(map(int,input('Enter elements of first list:').split())) |
29 |
| -b=list(map(int,input('Enter elements of second list:').split())) |
| 28 | +a = list(map(int, input("Enter elements of first list:").split())) |
| 29 | +b = list(map(int, input("Enter elements of second list:").split())) |
30 | 30 | # Example Input
|
31 |
| -#Enter elements of first list: 3 4 6 4 4 6 7 41 |
32 |
| -#Enter elements of second list: 78 3 5 7 -1 9 2 -5 |
| 31 | +# Enter elements of first list: 3 4 6 4 4 6 7 41 |
| 32 | +# Enter elements of second list: 78 3 5 7 -1 9 2 -5 |
33 | 33 |
|
34 | 34 |
|
35 |
| - |
36 |
| -'''bitwise or (|) between the sets of both arrays |
| 35 | +"""bitwise or (|) between the sets of both arrays |
37 | 36 | to find union and assign it into a variable A
|
38 | 37 | in the form of lists.
|
39 | 38 | bitwise and (&) between the sets of both arrays
|
40 | 39 | to find intersection and assign it into a variable A
|
41 | 40 | in the form of lists.
|
42 |
| -''' |
43 |
| - |
| 41 | +""" |
44 | 42 |
|
45 |
| -A=list(set(a)|set(b)) |
46 |
| -B=list(set(a)&set(b)) |
47 | 43 |
|
| 44 | +A = list(set(a) | set(b)) |
| 45 | +B = list(set(a) & set(b)) |
48 | 46 |
|
49 | 47 |
|
50 |
| -print('Union of the arrays:',A) |
51 |
| -print('intersection of the arrays:',B) |
| 48 | +print("Union of the arrays:", A) |
| 49 | +print("intersection of the arrays:", B) |
52 | 50 |
|
53 |
| -#Output |
54 |
| -'''Union of the arrays: [2, 3, 4, 5, 6, 7, 41, 9, 78, -5, -1] |
| 51 | +# Output |
| 52 | +"""Union of the arrays: [2, 3, 4, 5, 6, 7, 41, 9, 78, -5, -1] |
55 | 53 | intersection of the arrays: [3, 7]
|
56 |
| -''' |
| 54 | +""" |
0 commit comments