Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions data_structures/arrays/union_intersection_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'''
A list that has the common distinct element from both arrays and

Check failure on line 2 in data_structures/arrays/union_intersection_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/union_intersection_array.py:2:65: W291 Trailing whitespace

Check failure on line 2 in data_structures/arrays/union_intersection_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/union_intersection_array.py:2:65: W291 Trailing whitespace
if there are repetitions of the element then only one occurrence

Check failure on line 3 in data_structures/arrays/union_intersection_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/union_intersection_array.py:3:65: W291 Trailing whitespace

Check failure on line 3 in data_structures/arrays/union_intersection_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/union_intersection_array.py:3:65: W291 Trailing whitespace
is considered, known as the union of both arrays.

A list that has common distinct elements from both arrays,

Check failure on line 6 in data_structures/arrays/union_intersection_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/union_intersection_array.py:6:59: W291 Trailing whitespace

Check failure on line 6 in data_structures/arrays/union_intersection_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/union_intersection_array.py:6:59: W291 Trailing whitespace
is the intersection of both arrays.


Example:
Input: a[] = {1, 2, 2, 3, 4, 8, 10}, b[] = {4, 6, 3, 1}
Output: {1, 2, 3, 4, 6, 8, 10}
Explanation: 1, 2, 3, 4, 6, 8 and 10 is the union of

Check failure on line 13 in data_structures/arrays/union_intersection_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/union_intersection_array.py:13:53: W291 Trailing whitespace

Check failure on line 13 in data_structures/arrays/union_intersection_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/union_intersection_array.py:13:53: W291 Trailing whitespace
elements present in array a[] and array b[].

Input: a[] = {1, 2, 2, 3, 4, 8, 10}, b[] = {4, 6, 3, 1}
Output: {1, 2, 3, 4}
Explanation: 1, 2, 3, and 4 are the intersection(common elements)

Check failure on line 18 in data_structures/arrays/union_intersection_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/union_intersection_array.py:18:66: W291 Trailing whitespace

Check failure on line 18 in data_structures/arrays/union_intersection_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

data_structures/arrays/union_intersection_array.py:18:66: W291 Trailing whitespace
of elements present in array a[] and array b[].


'''

#Taking input lists from user

a=list(map(int,input('Enter elements of first list:').split()))
b=list(map(int,input('Enter elements of second list:').split()))
# Example Input
#Enter elements of first list: 3 4 6 4 4 6 7 41
#Enter elements of second list: 78 3 5 7 -1 9 2 -5



'''bitwise or (|) between the sets of both arrays
to find union and assign it into a variable A
in the form of lists.
bitwise and (&) between the sets of both arrays
to find intersection and assign it into a variable A
in the form of lists.
'''


A=list(set(a)|set(b))
B=list(set(a)&set(b))



print('Union of the arrays:',A)
print('intersection of the arrays:',B)

#Output
'''Union of the arrays: [2, 3, 4, 5, 6, 7, 41, 9, 78, -5, -1]
intersection of the arrays: [3, 7]
'''
Loading